1.一对一的关系关联
需求:用户和角色一对一关联
package com.example.jpa.pojo;
import javax.persistence.*;
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
/**
* CascadeType.PERSIST 只有User类新增时,
* 会级联Role对象新增。若Role对象在数据库存(跟新)在则抛异常(让Role变为持久态)
*/
@OneToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "role_id") //JoinColumn:维护一个外键的作用
private Role role;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
package com.example.jpa.pojo;
import javax.persistence.*;
@Entity
@Table(name = "t_role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToOne(mappedBy = "role")
private User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
/**
* 添加用户同时添加角色
*/
@Test
public void test1()
{
//创建角色
Role role =new Role();
role.setName("小角色");
//创建用户
User user =new User();
user.setName("小用户");
//建立关系
user.setRole(role);
role.setUser(user);
//保存数据
userRepository.save(user);
}
/**
* 根据用户id查询用户,同时查询用户角色
*/
@Test
public void test2()
{
User user =userRepository.getOne((long)13);
System.out.println(user);
Role role = user.getRole();
System.out.println(role);
}
2.一对多关联关系
需求:从角色到用户的一对多关联关系
package com.example.jpa.pojo;
import javax.persistence.*;
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "role_id")
private Role role;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
}
package com.example.jpa.pojo;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "t_role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "role")
private Set<User> userSet = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<User> getUserSet() {
return userSet;
}
public void setUserSet(Set<User> userSet) {
this.userSet = userSet;
}
}
/**
* 添加用户同时添加角色
*/
@Test
public void test1()
{
//创建角色
Role role =new Role();
role.setName("小角色");
//创建用户
User user =new User();
user.setName("小用户");
//建立关系
user.setRole(role);
role.getUserSet().add(user);
//保存数据
userRepository.save(user);
}
/**
* 根据用户id查询用户,同时查询用户角色
*/
@Test
public void test2()
{
User user =userRepository.getOne((long)13);
System.out.println(user);
Role role = user.getRole();
System.out.println(role);
}
3.多对多的关联关系
需求:用户和角色多对多关系
package com.example.jpa.pojo;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER) //fetch = FetchType.EAGER:异步加载
//JoinTable 配置中间表信息
//joinColumns 建立当前表在中间表中的外键字段
@JoinTable(name = "t_user_role",joinColumns = @JoinColumn(name = "user_id")
,inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles =new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Role> getRoles() {
return roles;
}
public void setRoles(Set<Role> roles) {
this.roles = roles;
}
}
package com.example.jpa.pojo;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "t_role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@ManyToMany(mappedBy = "roles")
private Set<User> users =new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
}
/**
* 添加用户同时添加角色
*/
@Test
public void test1()
{
//创建角色
Role role =new Role();
role.setName("小角色");
//创建用户
User user1 =new User();
user1.setName("小用户1");
User user2 =new User();
user2.setName("小用户2");
//建立关系
role.getUsers().add(user1);
role.getUsers().add(user2);
user1.getRoles().add(role);
user2.getRoles().add(role);
//保存数据
userRepository.save(user1);
userRepository.save(user2);
}
/**
* 根据用户id查询用户,同时查询用户所有角色
*/
@Test
public void test2()
{
User user =userRepository.getOne((long)13);
System.out.println(user);
Set<Role> roles = user.getRoles();
System.out.println(roles);
}