JPA
JPA全稱Java Persistence API.
JPA通過JDK 5.0注解或XML描述對象-關系表的映射關系,並將運行期的實體對象持久化到數據庫中。
Hibernate3.2+、TopLink(EclipseLink)以及OpenJPA都提供了JPA的實現。
搭建JPA開發環境
- JPA依賴的jar文件、數據庫驅動文件
- 配置文件:META-INF\persistence.xml
- 領域對象,POJO
- EntityManagerFactory
異常
javax.persistence.PersistenceException: No Persistence provider for EntityManager named com.jege.jpa
Hibernate從3.5開始必須添加hibernate-entitymanager相應jar文件支持JPA,否則使用JPA會出現如上異常
maven的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.jege.hibernate</groupId>
<artifactId>jpa-single-table</artifactId>
<version>1.0.0.RELEASE</version>
<packaging>jar</packaging>
<name>jpa-single-table</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>4.3.8.Final</hibernate.version>
<mysql.version>5.1.40</mysql.version>
<junit.version>4.12</junit.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- 必須添加 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>jpa-single-table</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
persistence.xml
JPA規范要求在類路徑的META-INF目錄下放置persistence.xml,文件的名稱是固定的
<persistence 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"
version="2.0">
<persistence-unit name="com.jege.jpa" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url" value="jdbc:mysql:///jpa" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="admin" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
JPA領域對象POJO
- 必須使用javax.persistence.Entity注解或者在XML映射文件中有對應的元素。
- 必須具有一個不帶參的構造函數,類不能聲明final,方法和持久化的屬性也不能聲明成final。並且持久化的屬性修飾符不能為public。
- 如果POJO實體需要以值的方式進行傳遞如Session bean,或者作為主鍵,則必須實現Serializable接口
源碼地址
如果覺得我的文章對您有幫助,請打賞支持。您的支持將鼓勵我繼續創作!謝謝!

