如果想要讓您所撰寫的持久層程 式符合JPA的標準,則可以使用Hibernate EntityManager,它實際是上Hibernate Core的Wrapper,提供了相容於JPA的程式設計API、實體物件生命週期、查詢語言等。
請先將Hibernate Core中lib下的javasist.jar加入Classpath,並下載Hibernate EntityManager,將其中的hibernate-entitymanager.jar與jboss-archive- browing.jar加入至Classpath中。
在JPA中,EntityManagerFactory物件的角色相當於Hibernate中SessionFactory的角色,您可以撰寫一個 JPAUtil類別,方便整個應用程式取得EntityManagerFactory物件:
- JPAUtil.java
package onlyfun.caterpillar;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static EntityManagerFactory entityManagerFactory;
static {
try {
entityManagerFactory =
Persistence.createEntityManagerFactory("example");
}
catch(Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static EntityManagerFactory getEntityManagerFactory() {
return entityManagerFactory;
}
public static void shutdown() {
getEntityManagerFactory().close();
}
}
EntityManagerFactory中的資料庫相關設定資訊與映射檔案設定資訊,稱之為一個持久單元 (persistence unit),這可以在Classpath的META-INF資料夾中persistence.xml加以定義:
- persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.23.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/ns/persistence
http://java.sun.com/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="example">
<properties>
<property name="hibernate.ejb.cfgfile"
value="/hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
<persistence-unit>的name屬性對應於 Persistence.createEntityManagerFactory ()方法所指定的名稱,在這邊先簡單的使用先前範例的hibernate.cfg.xml,當中包括了資料庫相關資訊與映射文件資訊。
JPA中EntityManager物件的角色則相當於Hibernate中Session的角色,要進行資料儲存之前,必須先取得 EntityTransaction,直接以程式示範如下:
- JPADemo.java
package onlyfun.caterpillar;
import java.util.List;
import javax.persistence.*;
public class JPADemo {
public static void main(String[] args) {
User user = new User();
user.setName("justin");
user.setAge(new Long(30));
EntityManager entityManager =
JPAUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction etx = entityManager.getTransaction();
etx.begin();
entityManager.persist(user);
etx.commit();
entityManager.close();
EntityManager newEntityManager =
JPAUtil.getEntityManagerFactory().createEntityManager();
EntityTransaction newEtx = newEntityManager.getTransaction();
newEtx.begin();
List users = newEntityManager.createQuery(
"select user from User user order by user.name asc")
.getResultList();
System.out.println(users.size() + " user(s) found.");
newEtx.commit();
newEntityManager.close();
JPAUtil.shutdown();
}
}
整個操作物件儲存與查詢的方式,與Hibernate非常的相似,如果要直接在persistence.xml中 設定資料庫設定等相關資訊,而不是載入hibernate.cfg.xml,則可以如下撰寫:
- persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.23.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/ns/persistence
http://java.sun.com/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="example">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection"
value="class, hbm"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost/demo"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="123456"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>