JPA(Java Persistence API)学习十三(级联)
1.级联操作
概述:在JPA中,如果对实体应用了任何操作,那么它只会在该特定实体上执行。
这些操作不适用于与其相关的其他实体。为了建立相关实体之间的依赖关系,
JPA提供了定义级联操作的javax.persistence.CascadeType
枚举类型。
这些级联操作可以用任何类型的映射来定义,即一对一,一对多,多对一,多对多。
级联枚举:
PERSIST: 在这个级联操作中,如果父实体持久存在,则其所有相关实体也将被持久化。
MERGE: 在这个级联操作中,如果父实体被合并,则其所有相关实体也将被合并。
DETACH: 在这个级联操作中,如果父实体被分离,那么它的所有相关实体也将被分离。
REFRESH 在此级联操作中,如果父实体被刷新,则其所有相关实体也将被刷新。
REMOVE: 在这个级联操作中,如果父实体被移除,则其所有相关实体也将被移除。
ALL : 在这种情况下,所有上述级联操作都可以应用于与父实体相关的实体。
2. JPA级联持久化
概述:级联持久化用于指定如果实体持久化,则其所有关联的子实体也将被持久化。
以下语法用于执行级联持久性操作
@OneToOne(cascade=CascadeType.PERSIST)
JPA级联持久化示例:
第一步:创建一个名为
StudentEntity.java
的实体类, 其中包含属性:
s_id
,s_name
,s_age
以及标记为级联规范的Subject
类型的对象。 代码:
import javax.persistence.*;
import com.yiibai.jpa.subject.Subject;
@Entity
@Table(name = "student")
public class StudentEntity {
@Table(name = "student")
public class StudentEntity {
@Id
private int s_id;
private String s_name;
private int s_age;
private int s_id;
private String s_name;
private int s_age;
@OneToOne(cascade = CascadeType.PERSIST)
private Subject sub;
private Subject sub;
}
第二步:创建另一个名为
Subject.java
的实体类 import javax.persistence.*;
@Entity
@Table(name = "subject")
public class Subject {
private String name;
private int marks;
@Id
private int s_id;
private int marks;
@Id
private int s_id;
}
第三步:将实体类和数据库配置映射到persistence.xml文件中
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Student_details">
<class>com.yiibai.jpa.student.StudentEntity</class>
<class>com.yiibai.jpa.subject.Subject</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="123456" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation"
value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Student_details">
<class>com.yiibai.jpa.student.StudentEntity</class>
<class>com.yiibai.jpa.subject.Subject</class>
<properties>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/testdb?serverTimezone=UTC" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password"
value="123456" />
<property name="eclipselink.logging.level" value="SEVERE" />
<property name="eclipselink.ddl-generation"
value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
3.应用
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Student_details");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.getTransaction().begin();
StudentEntity s1 = new StudentEntity();
s1.setS_id(101);
s1.setS_name("Maxsu");
s1.setS_age(20);
s1.setS_id(101);
s1.setS_name("Maxsu");
s1.setS_age(20);
StudentEntity s2 = new StudentEntity();
s2.setS_id(102);
s2.setS_name("James");
s2.setS_age(22);
s2.setS_id(102);
s2.setS_name("James");
s2.setS_age(22);
Subject sb1 = new Subject();
sb1.setName("ENGLISH");
sb1.setMarks(80);
sb1.setS_id(s1.getS_id());
sb1.setName("ENGLISH");
sb1.setMarks(80);
sb1.setS_id(s1.getS_id());
Subject sb2 = new Subject();
sb2.setName("Maths");
sb2.setMarks(75);
sb2.setS_id(s2.getS_id());
sb2.setName("Maths");
sb2.setMarks(75);
sb2.setS_id(s2.getS_id());
s1.setSub(sb1);
s2.setSub(sb2);
s2.setSub(sb2);
em.persist(s1);// No need to perform persist operation separately for different entities.
em.persist(s2);
em.persist(s2);
em.getTransaction().commit();
em.close();
emf.close();
emf.close();
学习来源:https://www.yiibai.com/jpa/jpa-cascade-persist.html#article-start