SpringDataJPA实体类关系

实体类关系

实体类关系
常用注解
常用属性
一对一 @OneToOne cascade:与此实体一对一关联的实体的联级样式类型
cascade = CascadeType.PERSIST(级联新建)
cascade = CascadeType.REMOVE(级联删除)
cascade = CascadeType.REFRESH(级联刷新)
cascade = CascadeType.MERGE(级联更新)
cascade = CascadeType.ALL(表示选择全部四项)
fetch:该实体的加载方式
fetch = FetchType.LAZY(懒加载)
fetch = FetchType.EAGER(立即加载)
targetEntity:默认关联的实体类型
targetEntity = className.class(className:关联的实体类)
mappedBy:用于双向关联实体,在一的一方进行声明(@ManyToOne没有此属性)
mappedBy="columnName"(columnName:另一方关联的字段名)
一对多 @OneToMany
多对一 @ManyToOne
多对多 @ManyToMany

单向一对多

说明:前者(一)维护关联关系
例:学生(Student)和学校(School)之间的关系

  • Student实体类
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "student") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) //标不需要转化为json的属性 
public class Student implements Serializable{

	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer studentId;
	private String studentName;
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}
  • School实体类
import java.io.Serializable;
import java.util.ArrayList;
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.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "school") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) //标不需要转化为json的属性 
public class School implements Serializable {
	
	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer schoolId;
	private String schoolName;
	@OneToMany // 一对多
	// 设置外键 name:关联字段名(无字段时自动生成)、referencedColumnName:主键字段名、,insertable = false, updatable = false:设置该字段只读
	@JoinColumn(name="sid",referencedColumnName="schoolId")
	private List<Student> student;
	
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}

单向多对一

说明:前者(多)维护关联关系
例:学生(Student)和学校(School)之间的关系

  • Student实体类
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "student") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) //标不需要转化为json的属性 
public class Student implements Serializable{

	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer studentId;
	private String studentName;
	@ManyToOne // 多对一
	// 设置外键 name:关联字段名(无字段时自动生成)、referencedColumnName:主键字段名、,insertable = false, updatable = false:设置该字段只读
	@JoinColumn(name="sid",referencedColumnName="schoolId")
	private School school;
	
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}
  • School实体类
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "school") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) //标不需要转化为json的属性 
public class School implements Serializable {
	
	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer schoolId;
	private String schoolName;
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}

双向 一对多 、多对一

说明:一对多 、多对一两者相结合
例:学生(Student)和学校(School)之间的关系

  • Student实体类
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "student") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) //标不需要转化为json的属性 
public class Student implements Serializable{

	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer studentId;
	private String studentName;
	@JsonIgnore // 标记在属性或者方法上,返回的json数据即不包含该属性
	@ManyToOne(targetEntity = School.class) 
	@JoinColumn(name="sid") // 设置外键
	private School school;
	
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}
  • School实体类
import java.io.Serializable;
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.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "school") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) //标不需要转化为json的属性 
public class School implements Serializable {
	
	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer schoolId;
	private String schoolName;
	@JsonIgnore // 标记在属性或者方法上,返回的json数据即不包含该属性
	// mappedBy="school" school对应Student实体类中"private School school;"的属性名
	@OneToMany(mappedBy="school",fetch=FetchType.EAGER,cascade=CascadeType.ALL) 
	private List<Student> student;
	
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}

双向多对多

例:用户(User)和角色(Role)之间的关系

  • User实体类
import java.io.Serializable;
import java.util.Set;

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.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "user") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })//标不需要转化为json的属性 
public class User implements Serializable{

	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	
	@Id //实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer uid;
	private String uname;
	private String upassword;
	@JsonIgnore // 标记在属性或者方法上,返回的json数据即不包含该属性
	@ManyToMany(fetch = FetchType.EAGER) // @ManyToMany:多对多、fetch = FetchType.EAGER:立即加载
	@Cascade(value={CascadeType.SAVE_UPDATE}) // 级联关系
	@JoinTable(name="user_role", // 指定第三张中间表名称,此表不需创建实体类
	 joinColumns={@JoinColumn(name="user_role_uid")}, // 本表主键 uid 与第三张中间表 user_role 的外键 user_role_uid 对应
	 inverseJoinColumns={@JoinColumn(name="user_role_rid")}) // 多对多关系另一张表与第三张中间表表的外键的对应关系
	// NotFoundAction.IGNORE找不到引用的外键数据时忽略、NotFoundAction.EXCEPTION:找不到元素时引发异常(默认和建议)
	@NotFound(action = NotFoundAction.IGNORE) 
	private Set<Role> role;
	
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}
  • Role实体类
import java.io.Serializable;
import java.util.Set;

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.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@Entity
@Table(name = "role") // 对应数据库的表名,没有表时自动创建该表
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" }) // 标不需要转化为json的属性 
public class Role implements Serializable {
	
	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	
	@Id // 实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) // 自动增长列
	private Integer rid;
	private String rname;
	@JsonIgnore // 标记在属性或者方法上,返回的json数据即不包含该属性
	@ManyToMany(fetch=FetchType.EAGER) // @ManyToMany:多对多关系、fetch = FetchType.EAGER:立即加载
	@Cascade(value={CascadeType.SAVE_UPDATE}) // 级联关系
	@JoinTable(name="user_role", // 指定第三张中间表名称,此表不需创建实体类
	 joinColumns={@JoinColumn(name="user_role_rid")}, // 本表主键 rid 与第三张中间表 user_role 的外键 user_role_rid 对应.本表与中间表的外键对应关系
	 inverseJoinColumns={@JoinColumn(name="user_role_uid")}) // 多对多关系另一张表与第三张中间表表的外键的对应关系
	// NotFoundAction.IGNORE找不到引用的外键数据时忽略、NotFoundAction.EXCEPTION:找不到元素时引发异常(默认和建议)
	@NotFound(action = NotFoundAction.IGNORE) 
	private Set<User> user; // 存放User列表
	
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}
发布了22 篇原创文章 · 获赞 0 · 访问量 2769

猜你喜欢

转载自blog.csdn.net/Xu_Ren/article/details/105148259