當前位置:
首頁 > 知識 > JPA Advanced Mappings(映射)

JPA Advanced Mappings(映射)

JPA Advanced Mappings(映射)

JPA是一個使用java規範發布的庫。因此,它支持所有面向對象的實體持久性概念。

繼承策略

繼承是面向對象語言的核心概念,因此我們可以在實體之間使用繼承關係或策略。JPA支持三種類型的繼承策略,如SINGLE_TABLE,JOINED_TABLE和TABLE_PER_CONCRETE_CLASS。

Staff, TeachingStaff, NonTeachingStaff關係:

JPA Advanced Mappings(映射)

單表策略

單表策略採用所有類欄位(超類和子類),並將其映射到稱為SINGLE_TABLE策略的單個表中。鑒別器值在區分一個表中三個實體的值時起關鍵作用。

讓我們考慮上面的例子,TeachingStaff和NonTeachingStaff是類員工的子類。提醒繼承的概念(是通過子類繼承超類的屬性的機制),因此sid,sname是屬於TeachingStaff和NonTeachingStaff的欄位。創建一個JPA項目。本項目的所有模塊如下:

創建實體

「src」包下創建一個名為「com.tutorialspoint.eclipselink.entity」的包。在給定的包下創建一個名為Staff.java的新Java類。Staff實體類顯示如下:

package com.tutorialspoint.eclipselink.entity;

import java.io.Serializable;

import javax.persistence.DiscriminatorColumn;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn( name = "type" )

public class Staff implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.AUTO )

private int sid;
private String sname;

public Staff( int sid, String sname ) {
super;
this.sid = sid;
this.sname = sname;
}

public Staff {
super;
}

public int getSid {
return sid;
}

public void setSid( int sid ) {
this.sid = sid;
}

public String getSname {
return sname;
}

public void setSname( String sname ) {
this.sname = sname;
}
}

在上述代碼中,@DescriminatorColumn指定欄位名稱(type),其值顯示剩餘的(Teaching and NonTeachingStaff)欄位。

在com.tutorialspoint.eclipselink.entity包下創建一個名為TeachingStaff.java的 Staff類的子類(class)。TeachingStaff實體類顯示如下:

package com.tutorialspoint.eclipselink.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue( value="TS" )
public class TeachingStaff extends Staff {

private String qualification;
private String subjectexpertise;

public TeachingStaff( int sid, String sname,

String qualification,String subjectexpertise ) {
super( sid, sname );
this.qualification = qualification;
this.subjectexpertise = subjectexpertise;
}

public TeachingStaff {
super;
}

public String getQualification{
return qualification;
}

public void setQualification( String qualification ){
this.qualification = qualification;
}

public String getSubjectexpertise {
return subjectexpertise;
}

public void setSubjectexpertise( String subjectexpertise ){
this.subjectexpertise = subjectexpertise;
}
}

在com.tutorialspoint.eclipselink.entity包下創建一個名為NonTeachingStaff.java的 Staff類的子類(class)。NonTeachingStaff實體類顯示如下:

package com.tutorialspoint.eclipselink.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue( value = "NS" )

public class NonTeachingStaff extends Staff {
private String areaexpertise;

public NonTeachingStaff( int sid, String sname, String areaexpertise ) {
super( sid, sname );
this.areaexpertise = areaexpertise;
}

public NonTeachingStaff {
super;
}

public String getAreaexpertise {
return areaexpertise;
}

public void setAreaexpertise( String areaexpertise ){
this.areaexpertise = areaexpertise;
}
}

persistence.xml中

Persistence.xml文件包含實體類的資料庫和註冊信息的配置信息。xml文件顯示如下:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="Eclipselink_JPA" transaction-type="RESOURCE_LOCAL">

<class>com.tutorialspoint.eclipselink.entity.Staff</class>
<class>com.tutorialspoint.eclipselink.entity.NonTeachingStaff</class>
<class>com.tutorialspoint.eclipselink.entity.TeachingStaff</class>

<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpadb"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>

</persistence-unit>
</persistence>

Service class

服務類是業務組件的實現部分。在名為"com.tutorialspoint.eclipselink.service"的"src"包下創建一個包。

在給定的包下創建一個名為SaveClient.java的類來存儲Staff,TeachingStaff和NonTeachingStaff類欄位。SaveClient類顯示如下:

package com.tutorialspoint.eclipselink.service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.tutorialspoint.eclipselink.entity.NonTeachingStaff;
import com.tutorialspoint.eclipselink.entity.TeachingStaff;

public class SaveClient {

public static void main( String[ ] args ) {

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );
EntityManager entitymanager = emfactory.createEntityManager;
entitymanager.getTransaction.begin;

//Teaching staff entity
TeachingStaff ts1=new TeachingStaff(1,"Gopal","MSc MEd","Maths");
TeachingStaff ts2=new TeachingStaff(2, "Manisha", "BSc BEd", "English");

//Non-Teaching Staff entity
NonTeachingStaff nts1=new NonTeachingStaff(3, "Satish", "Accounts");
NonTeachingStaff nts2=new NonTeachingStaff(4, "Krishna", "Office Admin");

//storing all entities
entitymanager.persist(ts1);
entitymanager.persist(ts2);
entitymanager.persist(nts1);
entitymanager.persist(nts2);

entitymanager.getTransaction.commit;

entitymanager.close;
emfactory.close;
}
}

編譯和執行上述程序後,您將在Eclipse IDE的控制面板中收到通知。檢查MySQL工作台的輸出。表格格式的輸出如下所示:


Sid Type Sname Areaexpertise Qualification Subjectexpertise
1 TS Gopal - MSC MED Maths
2 TS Manisha - BSC BED English
3 NS Satish Accounts
4 NS Krishna Office Admin

最後,您將獲得包含所有三類欄位的單表,並與名為「Type」(欄位)的discriminator列不同。

加盟表策略

加入表策略是共享引用的列,其中包含唯一值以加入表並進行簡單的事務。讓我們考慮與上述相同的例子。

創建JPA項目。所有項目模塊如下所示:

創建實體

在「src」包下創建一個名為「com.tutorialspoint.eclipselink.entity」 的包。在給定的包下創建一個名為Staff.java的新Java類。Staff實體類顯示如下:

package com.tutorialspoint.eclipselink.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table
@Inheritance( strategy = InheritanceType.JOINED )

public class Staff implements Serializable {

@Id
@GeneratedValue( strategy = GenerationType.AUTO )

private int sid;
private String sname;

public Staff( int sid, String sname ) {
super;
this.sid = sid;
this.sname = sname;
}

public Staff {
super;
}

public int getSid {
return sid;
}

public void setSid( int sid ) {
this.sid = sid;
}

public String getSname {
return sname;
}

public void setSname( String sname ) {
this.sname = sname;
}
}

package com.tutorialspoint.eclipselink.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@PrimaryKeyJoinColumn(referencedColumnName="sid")

public class TeachingStaff extends Staff {
private String qualification;
private String subjectexpertise;

public TeachingStaff( int sid, String sname,

String qualification,String subjectexpertise ) {
super( sid, sname );
this.qualification = qualification;
this.subjectexpertise = subjectexpertise;
}

public TeachingStaff {
super;
}

public String getQualification{
return qualification;
}

public void setQualification( String qualification ){
this.qualification = qualification;
}

public String getSubjectexpertise {
return subjectexpertise;
}

public void setSubjectexpertise( String subjectexpertise ){
this.subjectexpertise = subjectexpertise;
}
}

package com.tutorialspoint.eclipselink.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@PrimaryKeyJoinColumn(referencedColumnName="sid")

public class NonTeachingStaff extends Staff {
private String areaexpertise;

public NonTeachingStaff( int sid, String sname, String areaexpertise ) {
super( sid, sname );
this.areaexpertise = areaexpertise;
}

public NonTeachingStaff {
super;
}

public String getAreaexpertise {
return areaexpertise;
}

public void setAreaexpertise( String areaexpertise ) {
this.areaexpertise = areaexpertise;
}
}

persistence.xml中

<?xml version = "1.0" encoding = "UTF-8"?>

<persistence version = "2.0" xmlns = "http://java.sun.com/xml/ns/persistence"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name = "Eclipselink_JPA" transaction-type = "RESOURCE_LOCAL">
<class>com.tutorialspoint.eclipselink.entity.Staff</class>
<class>com.tutorialspoint.eclipselink.entity.NonTeachingStaff</class>
<class>com.tutorialspoint.eclipselink.entity.TeachingStaff</class>

<properties>
<property name = "javax.persistence.jdbc.url" value = "jdbc:mysql://localhost:3306/jpadb"/>
<property name = "javax.persistence.jdbc.user" value = "root"/>
<property name = "javax.persistence.jdbc.password" value = "root"/>
<property name = "javax.persistence.jdbc.driver" value = "com.mysql.jdbc.Driver"/>
<property name = "eclipselink.logging.level" value = "FINE"/>
<property name = "eclipselink.ddl-generation" value = "create-tables"/>
</properties>

</persistence-unit>
</persistence>

服務類

在給定的包下創建一個名為SaveClient.java的類來存儲Staff,TeachingStaff和NonTeachingStaff類欄位。然後SaveClient類如下:

package com.tutorialspoint.eclipselink.service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.tutorialspoint.eclipselink.entity.NonTeachingStaff;
import com.tutorialspoint.eclipselink.entity.TeachingStaff;

public class SaveClient {
public static void main( String[ ] args ) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );
EntityManager entitymanager = emfactory.createEntityManager;
entitymanager.getTransaction.begin;

//Teaching staff entity
TeachingStaff ts1 = new TeachingStaff(1,"Gopal","MSc MEd","Maths");
TeachingStaff ts2 = new TeachingStaff(2, "Manisha", "BSc BEd", "English");

//Non-Teaching Staff entity
NonTeachingStaff nts1 = new NonTeachingStaff(3, "Satish", "Accounts");
NonTeachingStaff nts2 = new NonTeachingStaff(4, "Krishna", "Office Admin");

//storing all entities
entitymanager.persist(ts1);
entitymanager.persist(ts2);
entitymanager.persist(nts1);
entitymanager.persist(nts2);

entitymanager.getTransaction.commit;
entitymanager.close;
emfactory.close;
}
}

編譯和執行上述程序後,您將在Eclipse IDE的控制面板中收到通知。輸出檢查MySQL工作台如下:

這裡創建了三個表格,表格格式的員工表格的結果如下所示:


Sid Dtype Sname
1 TeachingStaff Gopal
2 TeachingStaff Manisha
3 NonTeachingStaff Satish
4 NonTeachingStaff Krishna

TeachingStaff表的結果如表格所示:


Sid Qualification Subjectexpertise
1 MSC MED Maths
2 BSC BED English

在上表中sid是外鍵(參考欄位表單staff表)NonTeachingStaff表的結果如表格所示:


Sid Areaexpertise
3 Accounts
4 Office Admin

最後,這三個表分別使用它們的欄位創建,SID欄位由所有三個表共享。在員工表SID中是主鍵,在剩餘的(TeachingStaff和NonTeachingStaff)表中SID是外鍵。

每個類策略表

每個類策略的表是為每個子實體創建一個表。工作人員表將被創建,但它將包含空記錄。Staff表的欄位值必須由TeachingStaff和NonTeachingStaff表共享。

讓我們考慮與上述相同的例子。本項目的所有模塊如下所示:

package com.tutorialspoint.eclipselink.entity;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )

public class Staff implements Serializable {

@Id
@GeneratedValue( strategy = GenerationType.AUTO )

private int sid;
private String sname;

public Staff( int sid, String sname ) {
super;
this.sid = sid;
this.sname = sname;
}

public Staff {
super;
}

public int getSid {
return sid;
}

public void setSid( int sid ) {
this.sid = sid;
}

public String getSname {
return sname;
}

public void setSname( String sname ) {
this.sname = sname;
}
}

package com.tutorialspoint.eclipselink.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
public class TeachingStaff extends Staff {
private String qualification;
private String subjectexpertise;

public TeachingStaff( int sid, String sname, String qualification, String subjectexpertise ) {
super( sid, sname );
this.qualification = qualification;
this.subjectexpertise = subjectexpertise;
}

public TeachingStaff {
super;
}

public String getQualification{
return qualification;
}

public void setQualification( String qualification ) {
this.qualification = qualification;
}

public String getSubjectexpertise {
return subjectexpertise;
}

public void setSubjectexpertise( String subjectexpertise ){
this.subjectexpertise = subjectexpertise;
}
}

package com.tutorialspoint.eclipselink.entity;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
public class NonTeachingStaff extends Staff {
private String areaexpertise;

public NonTeachingStaff( int sid, String sname, String areaexpertise ) {
super( sid, sname );
this.areaexpertise = areaexpertise;
}

public NonTeachingStaff {
super;
}

public String getAreaexpertise {
return areaexpertise;
}

public void setAreaexpertise( String areaexpertise ) {
this.areaexpertise = areaexpertise;
}
}

<?xml version="1.0" encoding = "UTF-8"?>
<persistence version = "2.0" xmlns = "http://java.sun.com/xml/ns/persistence"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name = "Eclipselink_JPA" transaction-type = "RESOURCE_LOCAL">
<class>com.tutorialspoint.eclipselink.entity.Staff</class>
<class>com.tutorialspoint.eclipselink.entity.NonTeachingStaff</class>
<class>com.tutorialspoint.eclipselink.entity.TeachingStaff</class>

<properties>
<property name = "javax.persistence.jdbc.url" value = "jdbc:mysql://localhost:3306/jpadb"/>
<property name = "javax.persistence.jdbc.user" value = "root"/>
<property name = "javax.persistence.jdbc.password" value = "root"/>
<property name = "javax.persistence.jdbc.driver" value = "com.mysql.jdbc.Driver"/>
<property name = "eclipselink.logging.level" value = "FINE"/>
<property name = "eclipselink.ddl-generation" value="create-tables"/>
</properties>

</persistence-unit>
</persistence>

package com.tutorialspoint.eclipselink.service;

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

import com.tutorialspoint.eclipselink.entity.NonTeachingStaff;
import com.tutorialspoint.eclipselink.entity.TeachingStaff;

public class SaveClient {
public static void main( String[ ] args ) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "Eclipselink_JPA" );
EntityManager entitymanager = emfactory.createEntityManager;
entitymanager.getTransaction.begin;

//Teaching staff entity
TeachingStaff ts1 = new TeachingStaff(1,"Gopal","MSc MEd","Maths");
TeachingStaff ts2 = new TeachingStaff(2, "Manisha", "BSc BEd", "English");

//Non-Teaching Staff entity
NonTeachingStaff nts1 = new NonTeachingStaff(3, "Satish", "Accounts");
NonTeachingStaff nts2 = new NonTeachingStaff(4, "Krishna", "Office Admin");

//storing all entities
entitymanager.persist(ts1);
entitymanager.persist(ts2);
entitymanager.persist(nts1);
entitymanager.persist(nts2);

entitymanager.getTransaction.commit;
entitymanager.close;
emfactory.close;
}
}

編譯和執行上述程序後,您將在Eclipse IDE的控制面板中收到通知。對於輸出,檢查MySQL工作台如下:

這裡創建了三個表,Staff表包含空記錄。

TeachingStaff的結果如表格所示:


Sid Qualification Sname Subjectexpertise
1 MSC MED Gopal Maths
2 BSC BED Manisha English

上表TeachingStaff包含員工和教學實體的領域。

NonTeachingStaff的結果如表格所示:


Sid Areaexpertise Sname
3 Accounts Satish
4 Office Admin Krishna

以上表格NonTeachingStaff包含Staff和NonTeachingStaff實體的欄位。

公眾號:it全能程序猿

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

web組件工具之獲取表單數據:webUtils
第一章之s5pv210啟動順序
hadoop以及相關組件介紹以及個人理解
VMware workstation批量創建虛擬機和自動化安裝操作系統(一)

TAG:達人科技 |

您可能感興趣

Spring Boot與Kotlin使用Spring-data-jpa簡化數據訪問層
每日一博|一文讀懂JPA, Hibernate, Spring data jpa之間的關係
klg-jpa:spring-data-jpa 最佳實踐
spring+atomikos+mytatis+jpa實現分散式事務管理實戰篇
MySQL使用JPA+Hibernate的9個高性能技巧
SpringData JPA 在解析實體類欄位時駝峰自動添加下劃線問題
義大利藝術家SurajPatel風景油畫