JPA 级联持久化

级联持久化用于指定如果一个实体被持久化,那么它所有关联的子实体也将被持久化。以下语法用于执行级联持久化操作:

@OneToOne(cascade=CascadeType.PERSIST)  

JPA 级联持久化示例

在这个例子中,我们将创建两个相互关联的实体类,但为了建立它们之间的依赖关系,我们将执行级联操作。

项目结构如下:

pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yiidian</groupId>
    <artifactId>JPAProject</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>javax.persistence</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    </dependencies>
</project>

StudentEntity实体类

package com.yiidian.pojo;

import javax.persistence.*;

@Entity
@Table(name="student")
public class StudentEntity {  
  
    @Id
    private int s_id;  
    private String s_name;  
    private int s_age;  
      
    @OneToOne(cascade= CascadeType.PERSIST)
    private Subject sub;
      
    public Subject getSub() {  
        return sub;  
    }  
  
    public void setSub(Subject sub) {  
        this.sub = sub;  
    }  
  
    public StudentEntity(int s_id, String s_name, int s_age , Subject sub) {  
        super();  
        this.s_id = s_id;  
        this.s_name = s_name;  
        this.s_age = s_age;  
        this.sub=sub;  
    }  
  
    public StudentEntity() {  
        super();  
    }  
  
    public int getS_id() {  
        return s_id;  
    }  
  
    public void setS_id(int s_id) {  
        this.s_id = s_id;  
    }  
  
    public String getS_name() {  
        return s_name;  
    }  
  
    public void setS_name(String s_name) {  
        this.s_name = s_name;  
    }  
  
    public int getS_age() {  
        return s_age;  
    }  
  
    public void setS_age(int s_age) {  
        this.s_age = s_age;  
    }  
      
}  

Subject实体类

package com.yiidian.pojo;

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

@Entity
@Table(name="subject")
public class Subject {  

    private String name;  
    private int marks;  
      @Id
    private int s_id;  
    public Subject(String name, int marks, int s_id) {  
        super();  
        this.name = name;  
        this.marks = marks;  
            this.s_id=s_id;  
    }  
      
    public Subject()  
    {  
        super();  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getMarks() {  
        return marks;  
    }  
    public void setMarks(int marks) {  
        this.marks = marks;  
    }
    public int getS_id() {
        return s_id;  
    }
    public void setS_id(int s_id) {  
        this.s_id = s_id;  
    }  
      
}  

persistence.xml

在resources目录下创建META-INF目录,建立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.yiidian.pojo.StudentEntity</class>
        <class>com.yiidian.pojo.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/jpa?serverTimezone=UTC&amp;useSSL=false" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
        </properties>
    </persistence-unit>

</persistence>

StudentCascade测试类

package com.yiidian.test;

import com.yiidian.pojo.StudentEntity;
import com.yiidian.pojo.Subject;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class StudentCascade {
  
     public static void main( String[ ] args ) {  
             
          EntityManagerFactory emf = Persistence.createEntityManagerFactory( "Student_details" );
            
          EntityManager em = emf.createEntityManager( );
          em.getTransaction().begin();
           
          StudentEntity s1=new StudentEntity();
          s1.setS_id(101);  
          s1.setS_name("Jack");
          s1.setS_age(20);  
            
          StudentEntity s2=new StudentEntity();  
          s2.setS_id(102);  
          s2.setS_name("Rose");
          s2.setS_age(22);  
            
          Subject sb1=new Subject();
          sb1.setName("英语");
          sb1.setMarks(80);  
          sb1.setS_id(s1.getS_id());

          Subject sb2=new Subject();
          sb2.setName("数学");
          sb2.setMarks(75);  
          sb2.setS_id(s2.getS_id());
            
          s1.setSub(sb1);  
          s2.setSub(sb2);  

          em.persist( s1 );//No need to perform persist operation separately for different entities.  
          em.persist(s2);  
          
          em.getTransaction().commit();
          em.close( );  
          emf.close( );
    }  
  
      
}  

注意 : student 表中的主键,即 s_id 将被视为主题表中的外键,以维护两个表之间的关系。

程序执行后,在MySQL下生成了两张表。

  • student表 : 要获取数据,请在 MySQL 中运行select * from student

 

  • subject表 : 要获取数据,请在 MySQL 中运行select * from subject。

 

热门文章

优秀文章