Package jpa.controllers

Source Code of jpa.controllers.CommentratesJpaController

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jpa.controllers;

import entity.Commentrates;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Comments;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.IllegalOrphanException;
import jpa.controllers.exceptions.NonexistentEntityException;
import jpa.controllers.exceptions.PreexistingEntityException;

/**
*
* @author atap
*/
public class CommentratesJpaController implements Serializable {

    public CommentratesJpaController() {
    }
   
    public CommentratesJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

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

    public void create(Commentrates commentrates) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Comments commentsOrphanCheck = commentrates.getComments();
        if (commentsOrphanCheck != null) {
            Commentrates oldCommentratesOfComments = commentsOrphanCheck.getCommentrates();
            if (oldCommentratesOfComments != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Comments " + commentsOrphanCheck + " already has an item of type Commentrates whose comments column cannot be null. Please make another selection for the comments field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Comments comments = commentrates.getComments();
            if (comments != null) {
                comments = em.getReference(comments.getClass(), comments.getId());
                commentrates.setComments(comments);
            }
            em.persist(commentrates);
            if (comments != null) {
                comments.setCommentrates(commentrates);
                comments = em.merge(comments);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findCommentrates(commentrates.getId()) != null) {
                throw new PreexistingEntityException("Commentrates " + commentrates + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Commentrates commentrates) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Commentrates persistentCommentrates = em.find(Commentrates.class, commentrates.getId());
            Comments commentsOld = persistentCommentrates.getComments();
            Comments commentsNew = commentrates.getComments();
            List<String> illegalOrphanMessages = null;
            if (commentsNew != null && !commentsNew.equals(commentsOld)) {
                Commentrates oldCommentratesOfComments = commentsNew.getCommentrates();
                if (oldCommentratesOfComments != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Comments " + commentsNew + " already has an item of type Commentrates whose comments column cannot be null. Please make another selection for the comments field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (commentsNew != null) {
                commentsNew = em.getReference(commentsNew.getClass(), commentsNew.getId());
                commentrates.setComments(commentsNew);
            }
            commentrates = em.merge(commentrates);
            if (commentsOld != null && !commentsOld.equals(commentsNew)) {
                commentsOld.setCommentrates(null);
                commentsOld = em.merge(commentsOld);
            }
            if (commentsNew != null && !commentsNew.equals(commentsOld)) {
                commentsNew.setCommentrates(commentrates);
                commentsNew = em.merge(commentsNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = commentrates.getId();
                if (findCommentrates(id) == null) {
                    throw new NonexistentEntityException("The commentrates 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();
            Commentrates commentrates;
            try {
                commentrates = em.getReference(Commentrates.class, id);
                commentrates.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The commentrates with id " + id + " no longer exists.", enfe);
            }
            Comments comments = commentrates.getComments();
            if (comments != null) {
                comments.setCommentrates(null);
                comments = em.merge(comments);
            }
            em.remove(commentrates);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Commentrates> findCommentratesEntities() {
        return findCommentratesEntities(true, -1, -1);
    }

    public List<Commentrates> findCommentratesEntities(int maxResults, int firstResult) {
        return findCommentratesEntities(false, maxResults, firstResult);
    }

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

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

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

Related Classes of jpa.controllers.CommentratesJpaController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.