thufir

criteria

Oct 6th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. package romereader;
  2.  
  3. import java.io.Serializable;
  4. import java.util.List;
  5. import java.util.logging.Logger;
  6. import javax.persistence.EntityManager;
  7. import javax.persistence.EntityManagerFactory;
  8. import javax.persistence.Query;
  9. import javax.persistence.EntityNotFoundException;
  10. import javax.persistence.TypedQuery;
  11. import javax.persistence.criteria.CriteriaBuilder;
  12. import javax.persistence.criteria.CriteriaQuery;
  13. import javax.persistence.criteria.Root;
  14. import romereader.exceptions.NonexistentEntityException;
  15.  
  16. public class LinksDAO implements Serializable {
  17.  
  18.     private final static Logger log = Logger.getLogger(LinksDAO.class.getName());
  19.  
  20.     public LinksDAO(EntityManagerFactory emf) {
  21.         this.emf = emf;
  22.     }
  23.     private EntityManagerFactory emf = null;
  24.  
  25.     public EntityManager getEntityManager() {
  26.         return emf.createEntityManager();
  27.     }
  28.  
  29.     public List<Links> findLinkByParameter(Links l) {
  30.         EntityManager em = null;
  31.         List<Links> r;
  32.         try {
  33.             em = getEntityManager();
  34.             Query query = em.createNamedQuery("findLink");
  35.             query.setParameter("link", l.getLink());
  36.             r = query.getResultList();
  37.         } finally {
  38.             if (em != null) {
  39.                 em.close();
  40.             }
  41.         }
  42.         return r;
  43.     }
  44.  
  45.     public Links findSingle(Links link) {
  46.         EntityManager em = null;
  47.         Links singleResult = null;
  48.         try {
  49.             em = getEntityManager();
  50.             CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
  51.             CriteriaQuery criteriaQuery = criteriaBuilder.createQuery();
  52.             Root root = criteriaQuery.from(Links.class);
  53.             criteriaQuery.where(criteriaBuilder.equal(root.get("link"), criteriaBuilder.parameter(Long.class, "link")));
  54.             Query query = em.createQuery(criteriaQuery);
  55.             query.setParameter("link", link.getLink());
  56.             singleResult = (Links) query.getSingleResult();
  57.         } finally {
  58.             if (em != null) {
  59.                 em.close();
  60.             }
  61.         }
  62.         return singleResult;
  63.     }
  64.  
  65.     public Links findByLink(Links link) {
  66.         EntityManager em = null;
  67.         Links singleLink = null;
  68.         String linkString = link.getLink();
  69.         try {
  70.             em = getEntityManager();
  71.             TypedQuery<Links> query
  72.                     = em.createQuery("SELECT l FROM Links l WHERE l.link = :linkString", Links.class);
  73.             singleLink = query.getSingleResult();
  74.         } finally {
  75.             if (em != null) {
  76.                 em.close();
  77.             }
  78.         }
  79.         return singleLink;
  80.     }
  81.  
  82.     public void create(Links links) {
  83.         EntityManager em = null;
  84.         try {
  85.             em = getEntityManager();
  86.             em.getTransaction().begin();
  87.             em.persist(links);
  88.             em.getTransaction().commit();
  89.         } finally {
  90.             if (em != null) {
  91.                 em.close();
  92.             }
  93.         }
  94.     }
  95.  
  96.     public void edit(Links links) throws NonexistentEntityException, Exception {
  97.         EntityManager em = null;
  98.         try {
  99.             em = getEntityManager();
  100.             em.getTransaction().begin();
  101.             links = em.merge(links);
  102.             em.getTransaction().commit();
  103.         } catch (Exception ex) {
  104.             String msg = ex.getLocalizedMessage();
  105.             if (msg == null || msg.length() == 0) {
  106.                 Integer id = links.getId();
  107.                 if (findLinks(id) == null) {
  108.                     throw new NonexistentEntityException("The links with id " + id + " no longer exists.");
  109.                 }
  110.             }
  111.             throw ex;
  112.         } finally {
  113.             if (em != null) {
  114.                 em.close();
  115.             }
  116.         }
  117.     }
  118.  
  119.     public void destroy(Integer id) throws NonexistentEntityException {
  120.         EntityManager em = null;
  121.         try {
  122.             em = getEntityManager();
  123.             em.getTransaction().begin();
  124.             Links links;
  125.             try {
  126.                 links = em.getReference(Links.class, id);
  127.                 links.getId();
  128.             } catch (EntityNotFoundException enfe) {
  129.                 throw new NonexistentEntityException("The links with id " + id + " no longer exists.", enfe);
  130.             }
  131.             em.remove(links);
  132.             em.getTransaction().commit();
  133.         } finally {
  134.             if (em != null) {
  135.                 em.close();
  136.             }
  137.         }
  138.     }
  139.  
  140.     public List<Links> findLinksEntities() {
  141.         return findLinksEntities(true, -1, -1);
  142.     }
  143.  
  144.     public List<Links> findLinksEntities(int maxResults, int firstResult) {
  145.         return findLinksEntities(false, maxResults, firstResult);
  146.     }
  147.  
  148.     private List<Links> findLinksEntities(boolean all, int maxResults, int firstResult) {
  149.         EntityManager em = getEntityManager();
  150.         try {
  151.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  152.             cq.select(cq.from(Links.class));
  153.             Query q = em.createQuery(cq);
  154.             if (!all) {
  155.                 q.setMaxResults(maxResults);
  156.                 q.setFirstResult(firstResult);
  157.             }
  158.             return q.getResultList();
  159.         } finally {
  160.             em.close();
  161.         }
  162.     }
  163.  
  164.     public Links findLinks(Integer id) {
  165.         EntityManager em = getEntityManager();
  166.         try {
  167.             return em.find(Links.class, id);
  168.         } finally {
  169.             em.close();
  170.         }
  171.     }
  172.  
  173.     public int getLinksCount() {
  174.         EntityManager em = getEntityManager();
  175.         try {
  176.             CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
  177.             Root<Links> rt = cq.from(Links.class);
  178.             cq.select(em.getCriteriaBuilder().count(rt));
  179.             Query q = em.createQuery(cq);
  180.             return ((Long) q.getSingleResult()).intValue();
  181.         } finally {
  182.             em.close();
  183.         }
  184.     }
  185.  
  186. }
Advertisement
Add Comment
Please, Sign In to add comment