Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by AlDjabad ( 16 years ago )
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package loc.aldjabad.poster.ejb.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import loc.aldjabad.poster.ejb.entity.PlaceType;
import loc.aldjabad.poster.ejb.jpa.exceptions.NonexistentEntityException;
import loc.aldjabad.poster.ejb.jpa.exceptions.PreexistingEntityException;

/**
 *
 * @author aldjabad
 */
public class PlaceTypeJpaController {

    public PlaceTypeJpaController() {
        emf = Persistence.createEntityManagerFactory("poster-ejbPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PlaceType placeType) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(placeType);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPlaceType(placeType.getId()) != null) {
                throw new PreexistingEntityException("PlaceType " + placeType + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PlaceType placeType) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            placeType = em.merge(placeType);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = placeType.getId();
                if (findPlaceType(id) == null) {
                    throw new NonexistentEntityException("The placeType with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PlaceType placeType;
            try {
                placeType = em.getReference(PlaceType.class, id);
                placeType.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The placeType with id " + id + " no longer exists.", enfe);
            }
            em.remove(placeType);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PlaceType> findPlaceTypeEntities() {
        return findPlaceTypeEntities(true, -1, -1);
    }

    public List<PlaceType> findPlaceTypeEntities(int maxResults, int firstResult) {
        return findPlaceTypeEntities(false, maxResults, firstResult);
    }

    private List<PlaceType> findPlaceTypeEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(PlaceType.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PlaceType findPlaceType(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PlaceType.class, id);
        } finally {
            em.close();
        }
    }

    public int getPlaceTypeCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<PlaceType> rt = cq.from(PlaceType.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}

 

Revise this Paste

Your Name: Code Language: