java JPA中的EntityManager是怎樣的,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)主要從事做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)化德,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
JPA即Java Persistence API,是Java EE中針對(duì)持久化數(shù)據(jù)提供的規(guī)范。在使用JPA中,我們經(jīng)常會(huì)提到Entity,Entity就是在內(nèi)存中短暫存活,在數(shù)據(jù)庫(kù)中被持久化了的對(duì)象。Entity和數(shù)據(jù)庫(kù)中的表映射,也就是我們常說的ORM。我們可以持久化一個(gè)Entity,刪除一個(gè)Entity或者通過Java Persistence Query Language(JPQL)來查詢Entity。
通過注解的方式聲明一個(gè)entity如下:
Java代碼
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
private String title;
private Float price;
private String description;
private String isbn;
private Integer nbOfPage;
private Boolean illustrations;
// Getters, setters
}
Book Entity和數(shù)據(jù)庫(kù)的映射關(guān)系如圖:
在JPA中,所有的Entity都是通過javax.persistence.EntityManager的API來管理和操縱的。當(dāng)EntityManager管理Entity時(shí),所有的Entity都會(huì)有一個(gè)唯一標(biāo)識(shí)(這個(gè)標(biāo)識(shí)通常是主鍵列),Entity的狀態(tài)將會(huì)和數(shù)據(jù)庫(kù)同步。當(dāng)Entity脫離EntityManager的管理時(shí),Entity就變成了一個(gè)普通的Java對(duì)象實(shí)例,這時(shí)它的狀態(tài)是detached。
當(dāng)我們用new關(guān)鍵字創(chuàng)建一個(gè)新Entity時(shí),這個(gè)Entity對(duì)象存在于內(nèi)存中,JPA對(duì)它沒有任何了解。只有當(dāng)EntityManager開始管理它時(shí),它的狀態(tài)才會(huì)和數(shù)據(jù)庫(kù)同步。當(dāng)調(diào)用了EntityManager.remove方法后,它就會(huì)從數(shù)據(jù)庫(kù)中刪除掉,但Java對(duì)象還會(huì)在內(nèi)存中存在,直到被垃圾回收掉。
在我們介紹EntityManager API之前,我們先來看看Persistence Context的概念。一個(gè)Persistence Context就是針對(duì)一個(gè)事物中一段時(shí)間內(nèi)一群被管理的Entity的集合。多個(gè)具有相同唯一標(biāo)識(shí)的Entity實(shí)例不能存在于同一個(gè)Persistence Context中。例如,一個(gè)Book實(shí)例的ID是12,此時(shí)就不能有第二個(gè)ID也是12的Book實(shí)例存在于相同的Persistence Context中了。只有存在于Persistence Context中的Enitity才會(huì)被EntityManager所管理,它們的狀態(tài)才會(huì)反映到數(shù)據(jù)庫(kù)中。Persistence Context可以被看成一個(gè)一級(jí)緩存,它可以被EntityManager當(dāng)作存放Entity的緩存空間。默認(rèn)情況下,Entity在Persistence Context存活,直到用戶的事物結(jié)束。
每個(gè)事物用戶都有自己的Persistence Context,多個(gè)Persistence Context訪問同一個(gè)數(shù)據(jù)庫(kù)的實(shí)例如下圖:
我們可以調(diào)用EntityManager.persist()方法來持久化一個(gè)Entity,也就是向數(shù)據(jù)庫(kù)中插入數(shù)據(jù)。
Java代碼
Customer customer = new Customer("Antony", "Balla", "tballa@mail.com");
Address address = new Address("Ritherdon Rd", "London", "8QE", "UK");
customer.setAddress(address);
tx.begin();
em.persist(customer);
em.persist(address);
tx.commit();
上例中的Customer和Address是兩個(gè)普通的Java對(duì)象,當(dāng)被EntityManager調(diào)用了persist方法后,兩個(gè)對(duì)象都變成了EntityManager所管理的Entity。當(dāng)Transaction提交后,他們的數(shù)據(jù)會(huì)被插入到數(shù)據(jù)庫(kù)中。這里的Customer對(duì)象是對(duì)象關(guān)系的持有者,它對(duì)應(yīng)的表結(jié)構(gòu)應(yīng)當(dāng)有一個(gè)外鍵來對(duì)應(yīng)Address對(duì)象。
我們注意一下存儲(chǔ)兩個(gè)對(duì)象的順序。即便是將兩個(gè)對(duì)象存儲(chǔ)的順序顛倒一下,也不會(huì)造成外鍵找不到的錯(cuò)誤。之前我們已經(jīng)說過了,Persistence Context可以被看作一級(jí)緩存。在事物被提交之前,所有的數(shù)據(jù)都是在內(nèi)存中的,沒有對(duì)數(shù)據(jù)庫(kù)的訪問,EntityManager緩存了數(shù)據(jù),當(dāng)數(shù)據(jù)準(zhǔn)備好后,以底層數(shù)據(jù)庫(kù)期待的順序?qū)?shù)據(jù)更新到數(shù)據(jù)庫(kù)中。
想查找一個(gè)Entity,有兩個(gè)類似的方法,代碼如下:
Java代碼
Customer customer = em.find(Customer.class, 1234L)
if (customer!= null) {
// 處理對(duì)象
}
try {
Customer customer = em.getReference(Customer.class, 1234L)
// 處理對(duì)象
} catch(EntityNotFoundException ex) {
// Entity沒有找到
}
find方法會(huì)根據(jù)主鍵返回一個(gè)Entity,如果主鍵不存在數(shù)據(jù)庫(kù)中,會(huì)返回null。getReference和find方法很類似,但是只是返回一個(gè)Entity的引用,不會(huì)返回其中的數(shù)據(jù)。它用于那些我們需要一個(gè)Entity對(duì)象和它的主鍵但不需要具體數(shù)據(jù)的情況。如例所示,當(dāng)Entity找不到時(shí),會(huì)有EntityNotFoundException拋出。
一個(gè)Entity可以通過EntityManager.remove()被刪除,一但Entity被刪除,它在數(shù)據(jù)庫(kù)中也會(huì)被刪除,并且脫離了EntityManager管理(detached)。此時(shí)這個(gè)對(duì)象不能再和數(shù)據(jù)庫(kù)中的數(shù)據(jù)同步了。
Java代碼
tx.begin();
em.remove(customer);
tx.commit();
在之前的所有例子中,和數(shù)據(jù)庫(kù)的數(shù)據(jù)的同步都是發(fā)生在事物提交時(shí)。所待執(zhí)行的改變都是需要一個(gè)SQL語(yǔ)句的執(zhí)行。例如在下面的代碼中,兩條insert語(yǔ)句會(huì)在事物提交時(shí)被執(zhí)行的數(shù)據(jù)庫(kù)中。
Java代碼
tx.begin();
em.persist(customer);
em.persist(address);
tx.commit();
大多數(shù)情況下,這種和數(shù)據(jù)庫(kù)的同步機(jī)制能滿足我們程序的需要。如果我們想將對(duì)Persistence Context中數(shù)據(jù)改變立刻反映到數(shù)據(jù)庫(kù)中,可以通過調(diào)用flush方法實(shí)現(xiàn)。或者我們想將數(shù)據(jù)庫(kù)中的數(shù)據(jù)重新同步回Persistence Context,可以調(diào)用refresh方法。當(dāng)應(yīng)用程序在叫用了flush方法后,又調(diào)用了rollback方法,所有同步到數(shù)據(jù)庫(kù)的數(shù)據(jù)又會(huì)都被回滾。
這種同步機(jī)制很像我們?cè)趕qlplus中直接執(zhí)行多個(gè)SQL語(yǔ)句,當(dāng)顯性調(diào)用flush方法時(shí),相當(dāng)于執(zhí)行我們已經(jīng)輸入的SQL語(yǔ)句,但沒有提交事務(wù)。當(dāng)tx.commit方法調(diào)用時(shí),事物才真正的被提交。如果沒有調(diào)用flush方法,則在tx.commit方法調(diào)用時(shí)先執(zhí)行已經(jīng)輸入的SQL語(yǔ)句再提交事務(wù)。
Java代碼
tx.begin();
em.persist(customer);
em.flush();
em.persist(address);
tx.commit();
上面這個(gè)代碼例子中,persist執(zhí)行的順序是要被保證的。因?yàn)樵谡{(diào)用flush方法時(shí),變化已經(jīng)被同步到數(shù)據(jù)庫(kù)中了,即SQL語(yǔ)句已經(jīng)被執(zhí)行了,如果兩個(gè)persist方法順序顛倒一下,則會(huì)出現(xiàn)外鍵約束的異常。
refresh方法實(shí)現(xiàn)的效果可以通過下面的例子顯示出來:
Java代碼
Customer customer = em.find(Customer.class, 1234L)
assertEquals(customer.getFirstName(), "Antony");
customer.setFirstName("William");
em.refresh(customer);
assertEquals(customer.getFirstName(), "Antony");");
contains方法會(huì)返回一個(gè)Boolean值,用于檢測(cè)當(dāng)前Persistence Context中是否存在某個(gè)Entity
Java代碼
Customer customer = new Customer("Antony", "Balla", "tballa@mail.com");
tx.begin();
em.persist(customer);
tx.commit();
assertTrue(em.contains(customer));
tx.begin();
em.remove(customer);
tx.commit();
assertFalse(em.contains(customer));
clear方法可以清空當(dāng)前Persistence Context,是所有的Entity都變成detached狀態(tài)。detach方法則是只將某個(gè)Entity變成detached狀態(tài)。前面已經(jīng)說了detached的Entity不會(huì)和數(shù)據(jù)庫(kù)中的數(shù)據(jù)再進(jìn)行同步了。
Java代碼
Customer customer = new Customer("Antony", "Balla", "tballa@mail.com");
tx.begin();
em.persist(customer);
tx.commit();
assertTrue(em.contains(customer));
em.detach(customer);
assertFalse(em.contains(customer));
如果我們想使一個(gè)detached的Entity重新和數(shù)據(jù)庫(kù)中的數(shù)據(jù)進(jìn)行同步,可以調(diào)用merge方法。想象有這樣一個(gè)場(chǎng)景,我們需要從數(shù)據(jù)庫(kù)中取出某個(gè)對(duì)象,這個(gè)對(duì)象從持久層傳到表現(xiàn)層之前變成了detached狀態(tài)。在表現(xiàn)層中,Entity的一些數(shù)據(jù)發(fā)生了變化,我們將這個(gè)Entity傳回持久層并讓它變成managed狀態(tài)以將變化反映到數(shù)據(jù)庫(kù)中。
Java代碼
Customer customer = new Customer("Antony", "Balla", "tballa@mail.com");
tx.begin();
em.persist(customer);
tx.commit();
em.clear();
// 設(shè)置一個(gè)新的值給一個(gè)detached的entity
customer.setFirstName("William");
tx.begin();
em.merge(customer);
tx.commit();
最后我們通過一張圖來表示EntityManager對(duì)一個(gè)Entity的生命周期的改變。
關(guān)于 java JPA中的EntityManager是怎樣的問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
文章題目:javaJPA中的EntityManager是怎樣的
本文地址:http://www.chinadenli.net/article24/gechce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈、網(wǎng)站收錄、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)