持久層調用程式

package com.emprogria; import com.emprogria.exceptions.NonexistentEntityException; import com.emprogria.exceptions.PreexistingEntityException; import com.emprogria.exceptions.RollbackFailureException; import java.util.List; import javax.annotation.Resource; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.PersistenceUnit; import javax.persistence.Query; import javax.persistence.EntityNotFoundException; import javax.transaction.UserTransaction; public class ContactJpaController { @Resource private UserTransaction utx = null; @PersistenceUnit(unitName = "ContactPU") private EntityManagerFactory emf = null; public EntityManager getEntityManager() { return emf.createEntityManager(); } public void create(Contact contact) throws PreexistingEntityException, RollbackFailureException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); em.persist(contact); em.getTransaction().commit(); } catch (Exception ex) { if (findContact(contact.getMsn()) != null) { throw new PreexistingEntityException("通訊錄 " + contact + " 已存在", ex); } throw ex; } finally { if (em != null) { em.close(); } } } public void edit(Contact contact) throws NonexistentEntityException, RollbackFailureException, Exception { EntityManager em = null; try { em = getEntityManager(); em.getTransaction().begin(); contact = em.merge(contact); em.getTransaction().commit(); } catch (Exception ex) { String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { String id = contact.getMsn(); if (findContact(id) == null) { throw new NonexistentEntityException("資料:" + id + " 不存在"); } } throw ex; } finally { if (em != null) { em.close(); } } } public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception { EntityManager em = null; try { em = getEntityManager(); Contact contact; try { contact = em.getReference(Contact.class, id); contact.getMsn(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException("資料:" + id + " 不存在", enfe); } em.getTransaction().begin(); em.remove(contact); em.getTransaction().commit(); } catch (Exception ex) { throw ex; } finally { if (em != null) { em.close(); } } } public List<Contact> findContactEntities() { return findContactEntities(true, -1, -1); } public List<Contact> findContactEntities(int maxResults, int firstResult) { return findContactEntities(false, maxResults, firstResult); } private List<Contact> findContactEntities(boolean all, int maxResults, int firstResult) { EntityManager em = getEntityManager(); try { Query q = em.createQuery("select object(o) from Contact as o"); if (!all) { q.setMaxResults(maxResults); q.setFirstResult(firstResult); } return q.getResultList(); } finally { em.close(); } } public Contact findContact(String id) { EntityManager em = getEntityManager(); try { return em.find(Contact.class, id); } finally { em.close(); } } public int getContactCount() { EntityManager em = getEntityManager(); try { Query q = em.createQuery("select count(o) from Contact as o"); return ((Long) q.getSingleResult()).intValue(); } finally { em.close(); } } }