JPA 調用元件〈EJB〉

package com.emprogria.jpa;

import com.emprogria.jpa.CdrInfoPK;

import com.emprogria.exceptions.NonexistentEntityException;

import com.emprogria.exceptions.PreexistingEntityException;

import java.util.List;

import javax.persistence.EntityManager;

import javax.persistence.EntityManagerFactory;

import javax.persistence.Persistence;

import javax.persistence.Query;

import javax.persistence.EntityNotFoundException;

public class CdrInfoJpaController {

public CdrInfoJpaController() {

emf = Persistence.createEntityManagerFactory("CDR-Tasks-ejbPU");

}

private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {

return emf.createEntityManager();

}

public void create(CdrInfo cdrInfo) throws PreexistingEntityException, Exception {

if (cdrInfo.getCdrInfoPK() == null) {

cdrInfo.setCdrInfoPK(new CdrInfoPK());

}

EntityManager em = null;

try {

em = getEntityManager();

// em.getTransaction().begin();

em.persist(cdrInfo);

// em.getTransaction().commit();

} catch (Exception ex) {

if (findCdrInfo(cdrInfo.getCdrInfoPK()) != null) {

throw new PreexistingEntityException("物件 " + cdrInfo + " 已存在!", ex);

}

throw ex;

} finally {

if (em != null) {

em.close();

}

}

}

public void edit(CdrInfo cdrInfo) throws NonexistentEntityException, Exception {

EntityManager em = null;

try {

em = getEntityManager();

em.getTransaction().begin();

cdrInfo = em.merge(cdrInfo);

em.getTransaction().commit();

} catch (Exception ex) {

String msg = ex.getLocalizedMessage();

if (msg == null || msg.length() == 0) {

CdrInfoPK id = cdrInfo.getCdrInfoPK();

if (findCdrInfo(id) == null) {

throw new NonexistentEntityException("主鍵 " + id + " 不存在!");

}

}

throw ex;

} finally {

if (em != null) {

em.close();

}

}

}

public void destroy(CdrInfoPK id) throws NonexistentEntityException {

EntityManager em = null;

try {

em = getEntityManager();

em.getTransaction().begin();

CdrInfo cdrInfo;

try {

cdrInfo = em.getReference(CdrInfo.class, id);

cdrInfo.getCdrInfoPK();

} catch (EntityNotFoundException enfe) {

throw new NonexistentEntityException("主鍵 " + id + " 不存在!", enfe);

}

em.remove(cdrInfo);

em.getTransaction().commit();

} finally {

if (em != null) {

em.close();

}

}

}

public List<CdrInfo> findCdrInfoEntities() {

return findCdrInfoEntities(true, -1, -1);

}

public List<CdrInfo> findCdrInfoEntities(int maxResults, int firstResult) {

return findCdrInfoEntities(false, maxResults, firstResult);

}

private List<CdrInfo> findCdrInfoEntities(boolean all, int maxResults, int firstResult) {

EntityManager em = getEntityManager();

try {

Query q = em.createQuery("select object(o) from CdrInfo as o");

if (!all) {

q.setMaxResults(maxResults);

q.setFirstResult(firstResult);

}

return q.getResultList();

} finally {

em.close();

}

}

public CdrInfo findCdrInfo(CdrInfoPK id) {

EntityManager em = getEntityManager();

try {

return em.find(CdrInfo.class, id);

} finally {

em.close();

}

}

public int getCdrInfoCount() {

EntityManager em = getEntityManager();

try {

Query q = em.createQuery("select count(o) from CdrInfo as o");

return ((Long) q.getSingleResult()).intValue();

} finally {

em.close();

}

}

}