JPA级联删除

JPA级联删除

级联移除用于指定如果父实体被移除,则其所有相关实体也将被移除。 以下语法用于执行级联删除操作 -

@OneToOne(cascade=CascadeType.REMOVE)

JPA级联删除示例

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

这个例子包含以下步骤 -

第1步:com.zyiz.jpa.student包下创建一个名为StudentEntity.java的实体类,其中包含属性:s_ids_names_age以及标记为级联规范的Subject类型的对象。

文件: StudentEntity.java -

package com.zyiz.jpa.student;

import javax.persistence.*;
import com.zyiz.jpa.subject.Subject;

@Entity
@Table(name = "student")
public class StudentEntity {

    @Id
    private int s_id;
    private String s_name;
    private int s_age;

    @OneToOne(cascade = { CascadeType.REMOVE })
    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;
    }

}

第2步:com.zyiz.jpa.subject包下创建另一个名为Subject.java的实体类。

文件:Subject.java -

package com.zyiz.jpa.subject;

import javax.persistence.*;

@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文件中。

文件: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.zyiz.jpa.student.StudentEntity</class>  
    <class>com.zyiz.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>

com.zyiz.jpa.cascade包下创建一个名为StudentCascade.java的持久化类,以便将实体对象与数据保持一致。

文件:StudentCascade.java -

package com.zyiz.jpa.cascade;

import javax.persistence.*;
import com.zyiz.jpa.student.*;

public class StudentCascade {

    public static void main(String[] args) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Student_details");

        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();

        StudentEntity s = em.find(StudentEntity.class, 101);
        em.remove(s);

        em.getTransaction().commit();

        em.close();
        emf.close();

    }

}

注 - student表中的主键(即s_id)将作为subject表中的外键处理,以维持两个表之间的关系。

输出结果

程序执行完毕后,在MySQL工作台下生成以下表格。

  • student表 - 要获取数据,在MySQL中运行select * from student。得到结果如下 -

    mysql> select * from student;
    +------+-------+--------+----------+
    | S_ID | S_AGE | S_NAME | SUB_S_ID |
    +------+-------+--------+----------+
    |  102 |    22 | James  |      102 |
    +------+-------+--------+----------+
    rows in set
    
  • subject表 - 要获取数据,在MySQL中运行select * from subject。得到结果如下 -

    mysql>  select * from subject ;
    +------+-------+---------+
    | S_ID | MARKS | NAME    |
    +------+-------+---------+
    |  102 |    75 | Maths   |
    +------+-------+---------+
    rows in set