jpa 实体映射视图_JPA教程:实体映射-第3部分
jpa 實(shí)體映射視圖
在上一篇文章中,我展示了兩種讀取/寫入持久實(shí)體狀態(tài)的不同方法-字段和屬性。 使用字段訪問模式時(shí),JPA使用反射直接從實(shí)體的字段讀取狀態(tài)值。 如果我們沒有明確指定列名,它將直接將字段名轉(zhuǎn)換為數(shù)據(jù)庫(kù)列名。 在屬性訪問模式下,getter / setter方法用于讀取/寫入狀態(tài)值。 在這種情況下,我們使用相同的注釋來注釋實(shí)體狀態(tài)的getter方法,而不是字段。 如果我們未明確指定數(shù)據(jù)庫(kù)列名稱,則按照J(rèn)avaBean約定確定它們,即通過從getter方法名稱中刪除“ get”部分并將方法名稱其余部分的首字母轉(zhuǎn)換為小寫字符來確定。
我們可以通過在實(shí)體類聲明中使用@Access注釋來指定用于實(shí)體的訪問方式。 此注釋采用AccessType類型的參數(shù)(在javax.persistence包中定義)枚舉,該參數(shù)具有對(duì)應(yīng)于兩種不同訪問模式( FIELD和PROPERTY )的兩個(gè)不同值。 例如,我們可以通過以下方式為Address實(shí)體指定屬性訪問模式:
關(guān)于上述示例的幾點(diǎn)注意事項(xiàng):
如果一個(gè)實(shí)體沒有顯式的訪問模式信息,就像我們?cè)诒鞠盗械谝徊糠种袆?chuàng)建的Address實(shí)體一樣,那么JPA會(huì)采用默認(rèn)的訪問模式。 這個(gè)假設(shè)不是隨機(jī)的。 相反,JPA首先嘗試找出@Id批注的位置。 如果在字段上使用@Id批注,則假定為字段訪問模式。 如果在getter方法上使用@Id批注,則假定為屬性訪問模式。 因此,即使在上例中從地址實(shí)體中刪除@Access批注,映射仍將有效,并且JPA將采用屬性訪問模式:
@Entity @Table(name = "tbl_address") public class Address {private Integer id;private String street;private String city;private String province;private String country;private String postcode;private String transientColumn;@Id@GeneratedValue@Column(name = "address_id")public Integer getId() {return id;}// Rest of the class........有關(guān)訪問模式,需要記住的一些重要要點(diǎn):
也可以將這兩種訪問類型混合使用。 假設(shè)您要對(duì)實(shí)體的除一種狀態(tài)外的所有狀態(tài)都使用字段訪問模式,對(duì)于其余一種狀態(tài),您想使用屬性訪問模式,因?yàn)槟朐谙?從狀態(tài)值讀取/寫入狀態(tài)值之前執(zhí)行某種轉(zhuǎn)換。數(shù)據(jù)庫(kù)。 您可以按照以下步驟輕松完成此操作:
下面的示例演示了這種方法,因?yàn)閷⑧]政編碼更改為使用屬性訪問模式:
@Entity @Table(name = "tbl_address") @Access(AccessType.FIELD) public class Address {@Id@GeneratedValue@Column(name = "address_id")private Integer id;private String street;private String city;private String province;private String country;/*** postcode is now marked as Transient*/@Transientprivate String postcode;@Transientprivate String transientColumn;public Integer getId() {return id;}public Address setId(Integer id) {this.id = id;return this;}public String getStreet() {return street;}public Address setStreet(String street) {this.street = street;return this;}public String getCity() {return city;}public Address setCity(String city) {this.city = city;return this;}public String getProvince() {return province;}public Address setProvince(String province) {this.province = province;return this;}public String getCountry() {return country;}public Address setCountry(String country) {this.country = country;return this;}/*** We are now using property access mode for reading/writing* postcode*/@Access(AccessType.PROPERTY)public String getPostcode() {return postcode;}public Address setPostcode(String postcode) {this.postcode = postcode;return this;} }這里要注意的重要一點(diǎn)是,如果我們不使用@Access注釋對(duì)類進(jìn)行注釋,以將字段訪問模式顯式指定為默認(rèn)模式,而是對(duì)字段和getter方法進(jìn)行注釋,則映射的結(jié)果行為將是不確定的。 這意味著結(jié)果將完全取決于持久性提供程序,即一個(gè)提供程序可能選擇使用字段訪問模式作為默認(rèn)值,一個(gè)提供程序可能使用屬性訪問模式,或者一個(gè)可能決定引發(fā)異常!
今天就這樣。 如果您發(fā)現(xiàn)任何問題/有任何疑問,請(qǐng)隨時(shí)發(fā)表評(píng)論!
直到下一次。
翻譯自: https://www.javacodegeeks.com/2014/10/jpa-tutorial-mapping-entities-part-3.html
jpa 實(shí)體映射視圖
總結(jié)
以上是生活随笔為你收集整理的jpa 实体映射视图_JPA教程:实体映射-第3部分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (免费手机游戏下载安卓)
- 下一篇: 环保设备备案(除尘器备案)