日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

JPA教程–在Java SE环境中设置JPA

發布時間:2023/12/3 java 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JPA教程–在Java SE环境中设置JPA 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JPA代表Java Persistence API,它基本上是一個規范,描述了一種將數據持久存儲到持久存儲(通常是數據庫)中的方法。 我們可以將其視為類似于Hibernate的 ORM工具的東西,除了它是Java EE規范的正式組成部分(并且Java SE也支持它)。 有很多原因需要學習像JPA這樣的ORM工具。 我不會詳細介紹這個問題,因為網絡上已經有很多帖子可以完美地回答這個問題,例如這個問題或這個問題 。 但是,我們還應該記住, 這不是解決所有問題的靈丹妙藥 。
剛開始使用JPA時,我很難設置它,因為Web上的大多數文章都是針對Java EE環境編寫的,而我卻試圖在Java SE環境中使用它。 我希望本文對將來希望這樣做的人有所幫助。 在此示例中,我們將使用Maven設置所需的依賴關系。 由于JPA只是一個規范,因此我們還需要一個實現。 JPA有許多不錯的免費實現(例如EclipseLink,Hibernate等)。 在本文中,我選擇使用Hibernate。 至于數據庫,我將使用MySQL。 讓我們首先創建一個簡單的Maven項目。 我已經從命令行使用快速入門原型創建了我的。 如果您不知道該怎么做,可以按照本教程進行操作 。 好的,接下來讓我們獲取JPA的依賴項。 在pom.xml中包括以下幾行:

<dependency><groupId>javax.persistence</groupId><artifactId>persistence-api</artifactId><version>1.0.2</version> </dependency> <dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>4.3.6.Final</version><exclusions><exclusion><groupId>org.hibernate.javax.persistence</groupId><artifactId>hibernate-jpa-2.1-api</artifactId></exclusion></exclusions> </dependency>

第一個依賴關系指定標準的JPA接口,第二個依賴關系指定實現。 以這種方式包含JPA依賴關系是可取的,因為它使我們將??來可以自由切換特定于供應商的實現,而不會遇到太大問題( 請參閱此處的詳細信息 )。 但是,由于API版本1.0.2是作為獨立JAR發布的最后一個版本,因此我們將無法使用該API的最新版本。 在撰寫本文時,JPA規范的最新版本是2.1,尚無法獨立獲得(盡管有很多 要求 )。 如果我們現在想使用那個,那么我們唯一的選擇是從特定于供應商的JAR中選擇,或者使用提供API及其實現的應用服務器。 我決定使用Hibernate提供的API規范。 在這種情況下,僅包含以下依賴關系就足夠了:

<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>4.3.6.Final</version> </dependency>

下一步將包括對MySQL的依賴關系。 在pom.xml中包括以下幾行:

<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.31</version> </dependency>

包括其余依賴項(即jUnit,Hamcrest等)之后,完整的pom.xml如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.keertimaan.javasamples</groupId><artifactId>jpa-example</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>jpa-example</name><url>http://www.codesod.com</url><properties><java.version>1.8</java.version><hibernate.version>4.3.6.Final</hibernate.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- JPA --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-entitymanager</artifactId><version>${hibernate.version}</version></dependency><!-- For connection pooling --><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-c3p0</artifactId><version>${hibernate.version}</version></dependency><!-- Database --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.31</version></dependency><!-- Test --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope><exclusions><exclusion><groupId>org.hamcrest</groupId><artifactId>hamcrest-core</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-all</artifactId><version>1.3</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>2.5.1</version><configuration><source>${java.version}</source><target>${java.version}</target><compilerArgument>-Xlint:all</compilerArgument><showWarnings>true</showWarnings><showDeprecation>true</showDeprecation></configuration></plugin></plugins></build> </project>

現在是時候配置我們的數據庫了。 我將從這本出色的在線書中找到的所有將來的JPA示例中都將使用以下模式:

數據庫架構


在本地MySQL安裝中,按照上述模式創建一個等效的數據庫。 下一步是創建persistence.xml文件,該文件將包含我們的數據庫特定信息供JPA使用。 默認情況下,JPA希望此文件位于META-INF文件夾下的類路徑中。 對于我們的Maven項目,我在project_root / src / main / resources / META-INF文件夾下創建了此文件:

<persistence 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/persistencehttp://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"version="2.1"><persistence-unit name="jpa-example" transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.jpa.HibernatePersistenceProvider</provider><properties><property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_example" /><property name="javax.persistence.jdbc.user" value="root" /><property name="javax.persistence.jdbc.password" value="my_root_password" /><property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /><property name="hibernate.show_sql" value="true" /><property name="hibernate.format_sql" value="true" /><property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /><property name="hibernate.hbm2ddl.auto" value="validate" /><!-- Configuring Connection Pool --><property name="hibernate.c3p0.min_size" value="5" /><property name="hibernate.c3p0.max_size" value="20" /><property name="hibernate.c3p0.timeout" value="500" /><property name="hibernate.c3p0.max_statements" value="50" /><property name="hibernate.c3p0.idle_test_period" value="2000" /></properties></persistence-unit> </persistence>

如果您絕對是JPA的初學者,則上面的文件需要一些解釋。 在我的下一篇文章中,我將盡可能地解釋它,但是對于運行此示例,您只需要更改前三個屬性值以匹配您的環境(即數據庫名稱,用戶名和密碼)即可。 還要記下persistence-unit元素的name屬性的值。 此值稍后將在代碼中用于實例化EntityManagerFactory實例。
好的,現在讓我們創建一個實體來測試我們的配置。 創建一個具有以下內容的名為Address的類:

import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table;@Entity @Table(name = "address") public class Address {@Id@GeneratedValueprivate 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;} }

此類已正確映射到地址表,并且其實例已準備就緒,可以持久存儲在數據庫中。 現在,讓我們創建一個名為PersistenceManager的幫助程序類,其中包含以下內容:

import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence;public enum PersistenceManager {INSTANCE;private EntityManagerFactory emFactory;private PersistenceManager() {// "jpa-example" was the value of the name attribute of the// persistence-unit element.emFactory = Persistence.createEntityManagerFactory("jpa-example");}public EntityManager getEntityManager() {return emFactory.createEntityManager();}public void close() {emFactory.close();} }

現在讓我們在Main方法中編寫一些示例持久性代碼以測試所有內容:

import javax.persistence.EntityManager;public class Main {public static void main(String[] args) {Address address = new Address();address.setCity("Dhaka").setCountry("Bangladesh").setPostcode("1000").setStreet("Poribagh");EntityManager em = PersistenceManager.INSTANCE.getEntityManager();em.getTransaction().begin();em.persist(address);em.getTransaction().commit();em.close();PersistenceManager.INSTANCE.close();} }

如果您檢查數據庫,將會看到地址表中已插入新記錄。 本文介紹了如何在不使用任何其他框架(如Spring)的情況下設置JPA。 但是,使用Spring設置JPA是一個非常好的主意,因為在這種情況下,我們不必擔心管理實體管理器,事務等。除了設置JPA之外,Spring 對于許多其他目的也非常有用。 今天就這樣。 在下一篇文章中,我將嘗試解釋persistence.xml文件和相應的配置值。 敬請關注!

  • 完整的代碼可以在github上找到。

翻譯自: https://www.javacodegeeks.com/2014/08/jpa-tutorial-setting-up-jpa-in-a-java-se-environment.html

總結

以上是生活随笔為你收集整理的JPA教程–在Java SE环境中设置JPA的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。