环境搭建
源码地址:gitee:https://gitee.com/ytfs-dtx/JPA
导入依赖
创建配置文件
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> entity-manager-factory-ref="entityManagerFactoryBean"/>
创建实体类
注解说明
@ManyToMany
作用:用于映射多对多关系
属性:
cascade:配置级联操作。
fetch:配置是否采用延迟加载。
targetEntity:配置目标的实体类。映射多对多的时候不用写。
@JoinTable
作用:针对中间表的配置
属性:
nam:配置中间表的名称
joinColumns:中间表的外键字段关联当前实体类所对应表的主键字段
inverseJoinColumn:中间表的外键字段关联对方表的主键字段
@JoinColumn
作用:用于定义主键字段和外键字段的对应关系。
属性:
name:指定外键字段的名称
referencedColumnName:指定引用主表的主键字段名称
unique:是否唯一。默认值不唯一
nullable:是否允许为空。默认值允许。
insertable:是否允许插入。默认值允许。
updatable:是否允许更新。默认值允许。
columnDefinition:列的定义信息。
User
package xyz.ytfs.entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
* @author by ytfs
* @Classname User
* @Description TODO(用户类)
* @Date 2020/5/9 23:47
*/
@Entity
@Table(name = "sys_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long userId;
@Column(name = "user_name")
private String userName;
/**
* 配置用户到角色的多对多关系
* 配置多对多的映射关系
* 1.声明表关系的配置
*
* @ManyToMany(targetEntity = Role.class) //多对多
* targetEntity:代表对方的实体类字节码
* 2.配置中间表(包含两个外键)
* @JoinTable name : 中间表的名称
* joinColumns:配置当前对象在中间表的外键
* @JoinColumn的数组 name:外键名
* referencedColumnName:参照的主表的主键名
* inverseJoinColumns:配置对方对象在中间表的外键
*/
@ManyToMany(targetEntity = Role.class, cascade = CascadeType.ALL)
@JoinTable(name = "sys_user_role",
//joinColumns,当前对象在中间表中的外键
joinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")},
//inverseJoinColumns,对方对象在中间表的外键
inverseJoinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")})
private Set
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Set
return roles;
}
public void setRoles(Set
this.roles = roles;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", roles=" + roles +
'}';
}
}
Role
package xyz.ytfs.entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
/**
* @author by ytfs
* @Classname Role
* @Description TODO(用户对应的类型)
* @Date 2020/5/9 23:47
*/
@Entity
@Table(name = "sys_role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "role_id")
private Long roleId;
@Column(name = "role_name")
private String roleName;
/**
* 配置用户到角色的多对多关系
* 配置多对多的映射关系
* 1.声明表关系的配置
* @ManyToMany(targetEntity = User.class) //多对多
* targetEntity:代表对方的实体类字节码
* 2.配置中间表(包含两个外键)
* @JoinTable
* name : 中间表的名称
* joinColumns:配置当前对象在中间表的外键
* @JoinColumn的数组
* name:外键名
* referencedColumnName:参照的主表的主键名
* inverseJoinColumns:配置对方对象在中间表的外键
*/
/* @ManyToMany(targetEntity = User.class)
@JoinTable(name = "sys_user_role",
//joinColumns,当前对象在中间表中的外键
joinColumns = {@JoinColumn(name = "sys_role_id", referencedColumnName = "role_id")},
//inverseJoinColumns,对方对象在中间表的外键
inverseJoinColumns = {@JoinColumn(name = "sys_user_id", referencedColumnName = "user_id")})*/
@ManyToMany(mappedBy = "roles")
private Set
public Set
return users;
}
public void setUsers(Set
this.users = users;
}
public Long getRoleId() {
return roleId;
}
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
@Override
public String toString() {
return "Role{" +
"roleId=" + roleId +
", roleName='" + roleName + '\'' +
", users=" + users +
'}';
}
}
创建数据访问层的接口
IUserDao
package xyz.ytfs.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import xyz.ytfs.entity.User;
/**
* @author by ytfs
* @Classname UserDao
* @Description TODO(用户的数据访问层接口)
* @Date 2020/5/10 0:05
*/
public interface IUserDao extends JpaRepository
}
IRoleDao
package xyz.ytfs.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import xyz.ytfs.entity.Role;
/**
* @author by ytfs
* @Classname IRoleDao
* @Description TODO(用户对应的职位数据访问层)
* @Date 2020/5/10 0:07
*/
public interface IRoleDao extends JpaRepository
}
测试类
package xyz.ytfs.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import xyz.ytfs.dao.IRoleDao;
import xyz.ytfs.dao.IUserDao;
import xyz.ytfs.entity.Role;
import xyz.ytfs.entity.User;
import javax.transaction.Transactional;
import java.util.HashSet;
/**
* @author by ytfs
* @Classname JpaManyToManyTest
* @Description TODO(多对多的测试类)
* @Date 2020/5/10 0:08
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JpaManyToManyTest {
@Autowired
private IRoleDao roleDao;
@Autowired
private IUserDao userDao;
/**
* 保存一个用户,保存一个角色
*
* 多对多放弃维护权:被动的一方(这里就是被选择的一方Role)放弃
*/
@Test
@Transactional
@Rollback(false)
public void testSave() {
User user = new User();
user.setUserName("雨听风说");
Role role = new Role();
role.setRoleName("程序猿");
/*
表达多对多的关系,双方都表达的时候必须有一方放弃维护中间表,不然会抛出异常
*/
role.getUsers().add(user);
user.getRoles().add(role);
this.userDao.save(user);
this.roleDao.save(role);
}
//测试级联添加(保存一个用户的同时保存用户的关联角色)
@Test
@Transactional
@Rollback(false)
public void testCasacdeSave() {
User user = new User();
user.setUserName("雨听风说");
Role role = new Role();
role.setRoleName("程序猿");
/*
表达多对多的关系
*/
role.getUsers().add(user);
user.getRoles().add(role);
//级联操作,在保存用户的同时保存用户的类型
this.userDao.save(user);
}
/**
* 级联删除,删除id为1的客户
*/
@Test
@Transactional
@Rollback(false)
public void testDeleteCasaced() {
//查询id为1的用户
User user = this.userDao.getOne(1L);
//删除id为1的用户,级联删除用户对应的类型
this.userDao.delete(user);
}
}