Hibernate API和JPA API中主要接口之間有一些對應關係,例如:
JPA API ----------------------- Hibernate API
EntityManagerFactory ----- SessionFactory
EntityManager ---------------- Session
EntityTransaction ------------- Transactionjava
SessionFactory接口在JPA API中的對等接口是javax.persistence.EntityManagerFactory;Session接口在JPA API中的對等接口是javax.persistence.EntityManager;Transaction接口在JPA API中的對等接口是javax.persistence.EntityTransaction。Query接口在JPA API中的對等接口是javax.persistence.Query。sql
EntityManager接口提供了操縱數據庫的各類方法,如:
(1) persist()方法:把Java對象保存到數據庫中。等價於Session接口的persist()方法。
(2) merge()方法:保存或更新數據庫中的Java對象。等價於Session接口的merge()方法。
(3) remove()方法:把特定的Java對象從數據庫中刪除。相似於Session接口的delete()方法。EntityManager接口的remove()方法只能刪除持久化狀態的對象,而Session接口的delete()方法能夠刪除持久化狀態或遊離狀態的對象。。
(4) find()方法:從數據庫中加載Java對象。等價於Session接口的get()方法。
下面這個BusinessService類經過JPA API來訪問數據庫。,直接上源碼。數據庫
package mypack; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.EntityTransaction; import javax.persistence.Query; import java.io.*; import java.sql.Date; import java.sql.Timestamp; import java.util.*; public class BusinessService{ public static EntityManagerFactory entityManagerFactory; /** 初始化JPA,建立EntityManagerFactory實例 */ static{ try{ entityManagerFactory= Persistence.createEntityManagerFactory( "myunit" ); }catch(Exception e){ e.printStackTrace(); throw e; } } /** 查詢全部的Customer對象, 而後調用printCustomer()方法打印Customer對象信息 */ public void findAllCustomers(PrintWriter out)throws Exception{ EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); //開始一個事務 Query query=entityManager.createQuery( "from Customer as c order by c.name asc"); List customers=query.getResultList(); for (Iterator it = customers.iterator(); it.hasNext();) { printCustomer(out,(Customer) it.next()); } tx.commit(); //提交事務 }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { entityManager.close(); } } /** 持久化一個Customer對象 */ public void saveCustomer(Customer customer){ EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); entityManager.persist(customer); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { entityManager.close(); } } /** 按照OID加載一個Customer對象,而後修改它的屬性 */ public void loadAndUpdateCustomer(Long customer_id,String address){ EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); Customer c=entityManager .find(Customer.class,customer_id); c.setAddress(address); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { entityManager.close(); } } /**刪除Customer對象 */ public void deleteCustomer(Customer customer){ EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); //得到持久化狀態的Customer對象 Customer c=entityManager .find(Customer.class,customer.getId()); entityManager.remove(c); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { entityManager.close(); } } /** 把Customer對象的信息輸出到控制檯,如DOS 控制檯*/ private void printCustomer(PrintWriter out,Customer customer) throws Exception{……} public void test(PrintWriter out) throws Exception{……} public static void main(String args[]) throws Exception{ new BusinessService2().test(new PrintWriter(System.out,true)); entityManagerFactory.close(); } }
對JPA的初始化很是簡單,只要經過javax.persistence.Persistence的靜態方法createEntityManagerFactory()來建立EntityManagerFactory對象:markdown
entityManagerFactory= Persistence.createEntityManagerFactory( "myunit" );
以上Persistence.createEntityManagerFactory( 「myunit」 )方法中的參數「myunit」指定持久化單元包的名字。JPA會到persistence.xml配置文件中讀取相應的持久化單元包中的配置信息。
全部訪問數據庫的操做都使用如下流程:ide
EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction tx = null; try { tx = entityManager.getTransaction(); tx.begin(); //聲明開始事務 //執行查詢、保存、更新和刪除等各類數據訪問操做 …… tx.commit(); //提交事務 }catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; } finally { entityManager.close(); }