在使用Hibernate的时候我们常常会在类里边配置各种的关联关系,但是这个并不是很好配置,配置不当会出现各种各样的问题,下面具体来看一下:
首先我们来看User类里边有一个IdentityCard类,是个身份证类,那么用户和身份证就是一个一对一的关系,@OneToOne的映射关系。
package com.xz.springcloud.user;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import lombok.Data;
import lombok.ToString;
@Data
//@ToString
@Entity
@Table(name="s_user")
public class User {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String username;
private String password;
private Integer age;
private String gender;
private String address;
private String province;
private String city;
private String area;
private Integer state;
/**
* 注意看这里,双向一对第一
* 懒加载
*/
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn(name="identity_card")
private IdentityCard identityCard;
@JsonManagedReference
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="user_roles",joinColumns= {@JoinColumn(name="user_id")},inverseJoinColumns= {@JoinColumn(name="role_id")})
private List<Role> roles = new ArrayList<>();
}
接下来我们来看IdentityCard的定义
package com.xz.springcloud.user;
import java.io.Serializable;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data;
import lombok.ToString;
/**
* 身份证类
* @author yuxuan
*
*/
@Data
//@ToString
@Entity
@Table(name="s_identity_card")
public class IdentityCard implements Serializable{
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
//身份证编号
private String number;
@JsonBackReference
@OneToOne(mappedBy="identityCard")
private User user;
}
看到这里可能有的人会问@JsonBackReference是个什么鬼,这个注解是为了阻断循环依赖,或者使用@JsonIgnore也行。在双向一对一或者一多的时候会产生循环依赖的问题,就是我中有你,你中有我。会一直无限循环下去,主要防止在SpringMVC中JSON解析返回的时候会出现溢出异常。
另外我们来看下Role类的定义,他和User类似多对多的,一个用户可以对应多个角色,一个角色可以对应多个用户
package com.xz.springcloud.user;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonBackReference;
import lombok.Data;
import lombok.ToString;
@Data
//@ToString
@Entity
@Table(name="s_role")
public class Role {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private String roleName;
private Integer userId;
@JsonBackReference
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="user_roles",joinColumns= {@JoinColumn(name="role_id")},inverseJoinColumns= {@JoinColumn(name="user_id")})
private List<User> users = new ArrayList<>();
}
有问题可以在下面评论,技术问题可以私聊我。