Spring Data Jpa一对多单向映射
程序员文章站
2022-04-23 23:39:50
...
/**
- @author StormMaybin
- @date 2017-01-17
*/
生命不息,奋斗不止!
一对多映射关系
在JPA中,用@OneToMany来标识一对多的关系。实现一对多的单向关联,只需在代表一的实体(Company)中使用@OneToMany映射标注就可以了,代表多的实体不需要使用任何映射标注。
有两种方式实现一对多的单向关联。一种是在只使用@OneToMany来标识,这种方式是通过一张第三方表来保存关系。还有一种是使用@OneToMany和@JoinColumn来标注,这种方式是在多的一方(Employee)的表中增加一个外键列来保存关系。
OneToMany
Company.java
package com.stormma.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Created by mayongbin01 on 2017/1/17.
*/
@Entity
@Table(name = "company")
public class Company {
//唯一标识公司
@Id
@GeneratedValue
private int id;
//公司名字
private String name;
@OneToMany(cascade = {CascadeType.ALL})
private List<Employee> employees;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Employee.java
package com.stormma.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Created by mayongbin01 on 2017/1/17.
*/
@Entity
@Table(name = "employee")
public class Employee {
//唯一标识雇员
@Id
@GeneratedValue
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
生成的表结构
CREATE TABLE `employee` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `company`;
CREATE TABLE `company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `company_employees`;
CREATE TABLE `company_employees` (
`company_id` int(11) NOT NULL,
`employees_id` int(11) NOT NULL,
UNIQUE KEY `UK_lg2r1rg13q18sa62l1y7un4or` (`employees_id`),
KEY `FKd66w6jx84tydyd8sf9mpqh5je` (`company_id`),
CONSTRAINT `FKd66w6jx84tydyd8sf9mpqh5je` FOREIGN KEY (`company_id`) REFERENCES `company` (`id`),
CONSTRAINT `FKnntnqhhla66c4h9ddbnlvqk2x` FOREIGN KEY (`employees_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
OneToMany+JoinColumn
Company.java
package com.stormma.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* Created by mayongbin01 on 2017/1/17.
*/
@Entity
@Table(name = "company")
public class Company {
//唯一标识公司
@Id
@GeneratedValue
private int id;
//公司名字
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "company_id")
private List<Employee> employees;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Employee.java
package com.stormma.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Created by mayongbin01 on 2017/1/17.
*/
@Entity
@Table(name = "employee")
public class Employee {
//唯一标识雇员
@Id
@GeneratedValue
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
结构
Hibernate: create table company (id integer not null auto_increment, name varchar(255), primary key (id))
Hibernate: create table employee (id integer not null auto_increment, name varchar(255), company_id integer, primary key (id))
Hibernate: alter table employee add constraint FK5v50ed2bjh60n1gc7ifuxmgf4 foreign key (company_id) references company (id)
上一篇: aop +反射处理 service异常
下一篇: NIO
推荐阅读
-
JPA中实现单向一对多的关联关系
-
spring data jpa 使用记录 映射相关
-
JPA中映射关系详细说明(一对多,多对一,一对一、多对多)、@JoinColumn、mappedBy说明
-
JPA中实现单向一对多的关联关系
-
spring data jpa 的多对多映射关系
-
JPA单向多对一实体映射
-
Spring Data JPA处理一对一、一对多、多对多关系映射
-
spring data jpa配置一对多和多对一
-
Spring Data JPA 之 一对一,一对多,多对多 关系映射
-
spring data jpa 防止内存溢出的 一对多,多对多配置,加了@Data也没事,(其实是可以简单配置的)