JPA JOINED策略

在连接策略(JOINED)中,为每个实体类生成一个单独的表。每个表的属性都与主键连接。它消除了重复的可能性。

以下语法表示连接策略:

@Inheritance(strategy=InheritanceType.JOINED)

JOINED连接策略示例

在此示例中,我们将员工分为在职员工和退休员工。

项目目录结构如下:

Employee实体类

package com.yiidian.pojo;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "employee_details")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee implements Serializable {

    @Id
    private int e_id;
    private String e_name;

    public Employee(int e_id, String e_name) {
        super();
        this.e_id = e_id;
        this.e_name = e_name;
    }

    public Employee() {
        super();

    }

    public int getE_id() {
        return e_id;
    }

    public void setE_id(int e_id) {
        this.e_id = e_id;
    }

    public String getE_name() {
        return e_name;
    }

    public void setE_name(String e_name) {
        this.e_name = e_name;
    }

} 

RetiredEmployee实体类

package com.yiidian.pojo;

import javax.persistence.Entity;

@Entity
public class RetiredEmployee extends Employee {
    private int e_pension;
    public RetiredEmployee(int e_id, String e_name, int e_pension) {
        super(e_id, e_name);
        this.e_pension = e_pension;
    }

    public RetiredEmployee() {
        super();

    }

    public int getE_pension() {
        return e_pension;
    }

    public void setE_pension(int e_pension) {
        this.e_pension = e_pension;
    }

}  

ActiveEmployee实体类

package com.yiidian.pojo;

import javax.persistence.Entity;

@Entity
public class ActiveEmployee extends Employee {
    private int e_salary;
    private int e_experience;
    public ActiveEmployee(int e_id, String e_name, int e_salary, int e_experience) {
        super(e_id, e_name);
        this.e_salary = e_salary;
        this.e_experience = e_experience;
    }

    public ActiveEmployee() {
        super();

    }

    public int getE_salary() {
        return e_salary;
    }

    public void setE_salary(int e_salary) {
        this.e_salary = e_salary;
    }

    public int getE_experience() {
        return e_experience;
    }

    public void setE_experience(int e_experience) {
        this.e_experience = e_experience;
    }

}  

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="Employee_details">
        <class>com.yiidian.pojo.Employee</class>
        <class>com.yiidian.pojo.RetiredEmployee</class>
        <class>com.yiidian.pojo.ActiveEmployee</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>

EmployeePersistence测试类

package com.yiidian.test;

import com.yiidian.pojo.ActiveEmployee;
import com.yiidian.pojo.RetiredEmployee;

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

public class EmployeePersistence {

    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Employee_details");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();

        ActiveEmployee ae1 = new ActiveEmployee(101, "Eric", 10000, 5);
        ActiveEmployee ae2 = new ActiveEmployee(102, "Jack", 12000, 7);

        RetiredEmployee re1 = new RetiredEmployee(103, "Rose", 5000);
        RetiredEmployee re2 = new RetiredEmployee(104, "Lucy", 4000);

        em.persist(ae1);
        em.persist(ae2);

        em.persist(re1);
        em.persist(re2);

        em.getTransaction().commit();

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

程序执行后,在MySQL下生成如下目录层次结构。

现在,分别从每个表中获取数据以生成输出。

employee_details表:

retiredemployee表:

activeemployee表:

 

热门文章

优秀文章