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

一对一

例:公民(Person)和身份证(IdentityCard)

  • Person实体类
package demo.entity;

import java.io.Serializable;
import java.util.Date;

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.OneToOne;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

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

@Entity
@Table(name="person")//数据库的表名,如果数据库中没有此表,可自动生成
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })//标不需要转化为json的属性 
public class Person implements Serializable {
	
	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id //实体类的主键
	@GeneratedValue(strategy = GenerationType.IDENTITY) //自动增长列
	private Integer personId;
	private String personName;
	private String personGender;
	private String personNation;
	@JsonFormat(pattern="yyyy-MM-dd")
	@DateTimeFormat(pattern="yyyy-MM-dd") //格式化日期类型
	private Date personBirth;
	private String personAddress;
	@JsonIgnore // 标记在属性或者方法上,返回的json数据即不包含该属性
	@OneToOne(optional = false, mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.EAGER) //该表不建外键、optional属性:是否可以为空(默认值为:true)
	private IdentityCard identityCard;
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 Getter 和 Setter 方法 ------------------------
	//----------------------------- 此处省略了重写的 toString 方法 ------------------------
}
  • IdentityCard实体类
package demo.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

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

@Entity
@Table(name="identity_card")//数据库的表名,如果数据库中没有此表,可自动生成
@JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })//标不需要转化为json的属性 
public class IdentityCard implements Serializable {
	
	// 表明实现序列化类的不同版本间的兼容性
	private static final long serialVersionUID = 1L;
	@Id //实体类的主键
	private String cardId;
	private String cardName;
	private String cardGender;
	private String cardNation;
	@JsonFormat(pattern="yyyy-MM-dd")
	@DateTimeFormat(pattern="yyyy-MM-dd") //格式化日期类型
	private Date cardBirth;
	private String cardAddress;
	@JsonIgnore // 标记在属性或者方法上,返回的json数据即不包含该属性
	@OneToOne(cascade=CascadeType.ALL) // 一对一
	@JoinColumn(name="card_person_id",unique=true) // name:此表的外键关联字段
	private Person person;
	//----------------------------- 此处省略了 无参构造 和 有参构造 ------------------------
	//----------------------------- 此处省略了 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.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 方法 ------------------------
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值