JPA繼承-連接策略
在連接策略中,爲每個實體類生成一個單獨的表。 每個表的屬性都與主鍵連接。 它消除了字段字重複的可能性。
以下語法表示連接的策略: -
@Inheritance(strategy=InheritanceType.JOINED)
連接策略示例
在這個例子中,我們將員工分爲活躍員工和退休員工。
因此,子類ActiveEmployees
和RetiredEmployees
繼承父類Employee
的e_id
和e_name
字段。
現在,按照以下步驟創建JPA項目 -
第1步: 在com.yiibai.jpa.inheritence
包下創建一個根實體類Employee.java
並指定所有必需的屬性和註釋。
文件:Employee.java -
package com.yiibai.jpa.inheritence;
import java.io.Serializable;
import javax.persistence.*;
@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;
}
}
第2步: 在com.yiibai.jpa.inheritence
包下創建實體類ActiveEmployee.java
(它是Employee
類的子類)。
文件:ActiveEmployee.java -
package com.yiibai.jpa.inheritence;
import javax.persistence.*;
@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;
}
}
第3步: 在com.yiibai.jpa.inheritence
包下創建另一個實體類RetiredEmployee.java
(它是Employee.java
的子類)。
文件:RetiredEmployee.java -
package com.yiibai.jpa.inheritence;
import javax.persistence.*;
@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;
}
}
第4步: 將實體類和其他數據庫配置映射到Persistence.xml
文件中。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="Employee_details">
<class>com.yiibai.jpa.inheritence.ActiveEmployee</class>
<class>com.yiibai.jpa.inheritence.RetiredEmployee</class>
<class>com.yiibai.jpa.inheritence.Employee</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&characterEncoding=utf8" />
<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>
**第5步:**在com.yiibai.jpa.persistence
包下創建持久性類EmployeePersistence.java
。 這個類用於初始化一個對象並保存它。
文件: EmployeePersistence.java -
package com.yiibai.jpa.persistence;
import javax.persistence.*;
import com.yiibai.jpa.inheritence.*;
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, "李小云", 10000, 5);
ActiveEmployee ae2 = new ActiveEmployee(102, "張峯", 12000, 7);
RetiredEmployee re1 = new RetiredEmployee(103, "王四哥", 5000);
RetiredEmployee re2 = new RetiredEmployee(104, "葉問頂", 4000);
em.persist(ae1);
em.persist(ae2);
em.persist(re1);
em.persist(re2);
em.getTransaction().commit();
em.close();
emf.close();
}
}
執行程序後,在MySQL數據庫中運行select * from employee_details
查詢,將會得到以下輸出結果 -
mysql> select * from employee_details;
+------+-----------------+--------+
| E_ID | DTYPE | E_NAME |
+------+-----------------+--------+
| 101 | ActiveEmployee | 李小云 |
| 102 | ActiveEmployee | 張峯 |
| 104 | RetiredEmployee | 葉問頂 |
| 103 | RetiredEmployee | 王四哥 |
+------+-----------------+--------+
4 rows in set
查詢activeemployee
表,執行 Select * from activeemployee
-
mysql> Select * from activeemployee;
+------+--------------+----------+
| E_ID | E_EXPERIENCE | E_SALARY |
+------+--------------+----------+
| 101 | 5 | 10000 |
| 102 | 7 | 12000 |
+------+--------------+----------+
2 rows in set
查詢retired_employee
表,執行 Select * from retired_employee
-
mysql> Select * from retiredemployee;
+------+-----------+
| E_ID | E_PENSION |
+------+-----------+
| 104 | 4000 |
| 103 | 5000 |
+------+-----------+
2 rows in set