JPA級聯刪除

級聯移除用於指定如果父實體被移除,則其所有相關實體也將被移除。 以下語法用於執行級聯刪除操作 -

@OneToOne(cascade=CascadeType.REMOVE)

JPA級聯刪除示例

在這個例子中,我們將創建兩個相互關聯的實體類,但要建立它們之間的依賴關係,我們將執行級聯操作。

這個例子包含以下步驟 -

第1步:com.yiibai.jpa.student包下創建一個名爲StudentEntity.java的實體類,其中包含屬性:s_ids_names_age以及標記爲級聯規範的Subject類型的對象。

文件: StudentEntity.java -

package com.yiibai.jpa.student;

import javax.persistence.*;
import com.yiibai.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.yiibai.jpa.subject包下創建另一個名爲Subject.java的實體類。

文件:Subject.java -

package com.yiibai.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.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>

com.yiibai.jpa.cascade包下創建一個名爲StudentCascade.java的持久化類,以便將實體對象與數據保持一致。

文件:StudentCascade.java -

package com.yiibai.jpa.cascade;

import javax.persistence.*;
import com.yiibai.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 |
    +------+-------+--------+----------+
    1 rows in set
  • subject表 - 要獲取數據,在MySQL中運行select * from subject。得到結果如下 -

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