jpa 实体映射视图_JPA教程:映射实体–第1部分
jpa 實體映射視圖
在本文中,我將討論JPA中的實體映射過程。 至于我的示例,我將使用與 我以前的一篇文章中使用的模式相同的模式 。
在前兩篇文章中,我解釋了如何在Java SE環境中設置JPA。 我不打算為Web應用程序編寫設置過程,因為Web上的大多數教程都完全做到了這一點。 因此,讓我們直接跳過對象關系映射或實體映射。
Wikipedia定義對象關系映射如下:
計算機科學中的對象關系映射(ORM,O / RM和O / R映射)是一種編程技術,用于在面向對象的編程語言中的不兼容類型系統之間轉換數據。 實際上,這創建了一個“虛擬對象數據庫”,可以在編程語言中使用它。 盡管有些程序員選擇創建自己的ORM工具,但有執行對象關系映射的免費和商業軟件包。
通常,映射是您將有關數據庫的必要信息提供給ORM工具的過程。 然后,該工具使用此信息將對象讀/寫到數據庫中。 通常,您告訴您的ORM工具表名稱,特定類型的對象將保存到該表名稱。 您還提供對象屬性將映射到的列名。 還需要指定不同對象類型之間的關系。 所有這些似乎都是艱巨的任務,但是幸運的是,JPA遵循了所謂的“ Convention over Configuration”方法,這意味著如果您采用JPA提供的默認值,則只需配置很少的部分應用程序。
為了在JPA中正確映射類型,您至少需要執行以下操作:
就是這樣。 您的實體已準備好保存到數據庫中,因為JPA會自動配置映射的所有其他方面。 這也顯示了使用JPA可以享受的生產率提高。 您無需在每次查詢數據庫時手動填充對象,從而無需編寫大量樣板代碼。
讓我們來看一個例子。 考慮以下我根據以上兩個規則映射的地址實體:
import javax.persistence.Entity; import javax.persistence.Id;@Entity public class Address {@Idprivate Integer id;private String street;private String city;private String province;private String country;private String postcode;/*** @return the id*/public Integer getId() {return id;}/*** @param id the id to set*/public Address setId(Integer id) {this.id = id;return this;}/*** @return the street*/public String getStreet() {return street;}/*** @param street the street to set*/public Address setStreet(String street) {this.street = street;return this;}/*** @return the city*/public String getCity() {return city;}/*** @param city the city to set*/public Address setCity(String city) {this.city = city;return this;}/*** @return the province*/public String getProvince() {return province;}/*** @param province the province to set*/public Address setProvince(String province) {this.province = province;return this;}/*** @return the country*/public String getCountry() {return country;}/*** @param country the country to set*/public Address setCountry(String country) {this.country = country;return this;}/*** @return the postcode*/public String getPostcode() {return postcode;}/*** @param postcode the postcode to set*/public Address setPostcode(String postcode) {this.postcode = postcode;return this;} }現在,根據您的環境,您可能會或可能不會在persistence.xml文件中添加此實體聲明,我在上一篇文章中已經對此進行了解釋。
好吧,讓我們保存一些對象! 以下代碼段正是這樣做的:
import com.keertimaan.javasamples.jpaexample.entity.Address; import javax.persistence.EntityManager; import com.keertimaan.javasamples.jpaexample.persistenceutil.PersistenceManager;public class Main {public static void main(String[] args) {EntityManager em = PersistenceManager.INSTANCE.getEntityManager();Address address = new Address().setId(1).setCity("Dhaka").setCountry("Bangladesh").setPostcode("1000").setStreet("Poribagh");em.getTransaction().begin();em.persist(address);em.getTransaction().commit();System.out.println("addess is saved! It has id: " + address.getId());Address anotherAddress = new Address().setId(2).setCity("Shinagawa-ku, Tokyo").setCountry("Japan").setPostcode("140-0002").setStreet("Shinagawa Seaside Area");em.getTransaction().begin();em.persist(anotherAddress);em.getTransaction().commit();em.close();System.out.println("anotherAddress is saved! It has id: " + anotherAddress.getId());PersistenceManager.INSTANCE.close();} }現在讓我們退后一步,考慮一下如果使用純JDBC進行持久化,我們需要做些什么。 對于這兩種情況,我們必須手動編寫插入查詢并將每個屬性映射到相應的列,這將需要大量的代碼。
關于該示例要注意的重要一點是我設置實體ID的方式。 這種方法僅適用于像這樣的簡短示例,但是對于實際應用而言,這不是很好。 通常,您通常希望使用自動遞增的id列或數據庫序列來生成實體的id值。 在我的示例中,我使用的是MySQL數據庫,并且我所有的id列均設置為自動遞增。 為了在實體模型中體現這一點,我可以在id屬性中使用一個名為@GeneratedValue的附加注釋。 這告訴JPA,該實體的id值將在插入期間由數據庫自動生成,并且應該在插入之后使用select命令獲取該id。
經過上述修改,我的實體類變成了這樣的東西:
import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue;@Entity public class Address {@Id@GeneratedValueprivate Integer id;// Rest of the class code........插入過程變為:
Address anotherAddress = new Address().setCity("Shinagawa-ku, Tokyo").setCountry("Japan").setPostcode("140-0002").setStreet("Shinagawa Seaside Area"); em.getTransaction().begin(); em.persist(anotherAddress); em.getTransaction().commit();JPA如何確定用于保存地址實體的表? 事實證明,這很簡單:
那么,JPA如何確定我們用于保存地址實體屬性值的列?
在這一點上,我認為您將能夠輕松地猜測到這一點。 如果您不能,請繼續關注我的下一篇文章!
直到下一次。
[完整的工作代碼可以在github上找到。]
翻譯自: https://www.javacodegeeks.com/2014/09/jpa-tutorial-mapping-entities-part-1.html
jpa 實體映射視圖
總結
以上是生活随笔為你收集整理的jpa 实体映射视图_JPA教程:映射实体–第1部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生学堂家长端电脑版下载安装(生学堂家长端
- 下一篇: 电脑全屏拍照软件(可以全屏拍照的软件)