|
| 1 | +package unit15.jpa.Actividad16_Aplicacion; |
| 2 | + |
| 3 | +import unit15.jpa.Actividad07.exceptions.NonexistentEntityException; |
| 4 | +import unit15.jpa.Actividad07.exceptions.PreexistingEntityException; |
| 5 | + |
| 6 | +import java.io.Serializable; |
| 7 | +import java.util.List; |
| 8 | +import javax.persistence.EntityManager; |
| 9 | +import javax.persistence.EntityManagerFactory; |
| 10 | +import javax.persistence.Query; |
| 11 | +import javax.persistence.EntityNotFoundException; |
| 12 | +import javax.persistence.criteria.CriteriaQuery; |
| 13 | +import javax.persistence.criteria.Root; |
| 14 | + |
| 15 | +public class AlumnoDAO implements Serializable { |
| 16 | + |
| 17 | + public AlumnoDAO(EntityManagerFactory emf) { |
| 18 | + this.emf = emf; |
| 19 | + } |
| 20 | + private EntityManagerFactory emf = null; |
| 21 | + |
| 22 | + public EntityManager getEntityManager() { |
| 23 | + return emf.createEntityManager(); |
| 24 | + } |
| 25 | + |
| 26 | + public void create(Alumno alumno) throws PreexistingEntityException, Exception { |
| 27 | + EntityManager em = null; |
| 28 | + try { |
| 29 | + em = getEntityManager(); |
| 30 | + em.getTransaction().begin(); |
| 31 | + Portatil portatil = alumno.getPortatil(); |
| 32 | + if (portatil != null) { |
| 33 | + portatil = em.getReference(portatil.getClass(), portatil.getId()); |
| 34 | + alumno.setPortatil(portatil); |
| 35 | + } |
| 36 | + em.persist(alumno); |
| 37 | + if (portatil != null) { |
| 38 | + Alumno oldAlumnoOfPortatil = portatil.getAlumno(); |
| 39 | + if (oldAlumnoOfPortatil != null) { |
| 40 | + oldAlumnoOfPortatil.setPortatil(null); |
| 41 | + oldAlumnoOfPortatil = em.merge(oldAlumnoOfPortatil); |
| 42 | + } |
| 43 | + portatil.setAlumno(alumno); |
| 44 | + portatil = em.merge(portatil); |
| 45 | + } |
| 46 | + em.getTransaction().commit(); |
| 47 | + } catch (Exception ex) { |
| 48 | + if (findAlumno(alumno.getNum()) != null) { |
| 49 | + throw new PreexistingEntityException("Alumno " + alumno + " already exists.", ex); |
| 50 | + } |
| 51 | + throw ex; |
| 52 | + } finally { |
| 53 | + if (em != null) { |
| 54 | + em.close(); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void edit(Alumno alumno) throws NonexistentEntityException, Exception { |
| 60 | + EntityManager em = null; |
| 61 | + try { |
| 62 | + em = getEntityManager(); |
| 63 | + em.getTransaction().begin(); |
| 64 | + Alumno persistentAlumno = em.find(Alumno.class, alumno.getNum()); |
| 65 | + Portatil portatilOld = persistentAlumno.getPortatil(); |
| 66 | + Portatil portatilNew = alumno.getPortatil(); |
| 67 | + if (portatilNew != null) { |
| 68 | + portatilNew = em.getReference(portatilNew.getClass(), portatilNew.getId()); |
| 69 | + alumno.setPortatil(portatilNew); |
| 70 | + } |
| 71 | + alumno = em.merge(alumno); |
| 72 | + if (portatilOld != null && !portatilOld.equals(portatilNew)) { |
| 73 | + portatilOld.setAlumno(null); |
| 74 | + portatilOld = em.merge(portatilOld); |
| 75 | + } |
| 76 | + if (portatilNew != null && !portatilNew.equals(portatilOld)) { |
| 77 | + Alumno oldAlumnoOfPortatil = portatilNew.getAlumno(); |
| 78 | + if (oldAlumnoOfPortatil != null) { |
| 79 | + oldAlumnoOfPortatil.setPortatil(null); |
| 80 | + oldAlumnoOfPortatil = em.merge(oldAlumnoOfPortatil); |
| 81 | + } |
| 82 | + portatilNew.setAlumno(alumno); |
| 83 | + portatilNew = em.merge(portatilNew); |
| 84 | + } |
| 85 | + em.getTransaction().commit(); |
| 86 | + } catch (Exception ex) { |
| 87 | + String msg = ex.getLocalizedMessage(); |
| 88 | + if (msg == null || msg.length() == 0) { |
| 89 | + int id = alumno.getNum(); |
| 90 | + if (findAlumno(id) == null) { |
| 91 | + throw new NonexistentEntityException("The alumno with id " + id + " no longer exists."); |
| 92 | + } |
| 93 | + } |
| 94 | + throw ex; |
| 95 | + } finally { |
| 96 | + if (em != null) { |
| 97 | + em.close(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void destroy(int id) throws NonexistentEntityException { |
| 103 | + EntityManager em = null; |
| 104 | + try { |
| 105 | + em = getEntityManager(); |
| 106 | + em.getTransaction().begin(); |
| 107 | + Alumno alumno; |
| 108 | + try { |
| 109 | + alumno = em.getReference(Alumno.class, id); |
| 110 | + alumno.getNum(); |
| 111 | + } catch (EntityNotFoundException enfe) { |
| 112 | + throw new NonexistentEntityException("The alumno with id " + id + " no longer exists.", enfe); |
| 113 | + } |
| 114 | + Portatil portatil = alumno.getPortatil(); |
| 115 | + if (portatil != null) { |
| 116 | + portatil.setAlumno(null); |
| 117 | + portatil = em.merge(portatil); |
| 118 | + } |
| 119 | + em.remove(alumno); |
| 120 | + em.getTransaction().commit(); |
| 121 | + } finally { |
| 122 | + if (em != null) { |
| 123 | + em.close(); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public List<Alumno> findAlumnoEntities() { |
| 129 | + return findAlumnoEntities(true, -1, -1); |
| 130 | + } |
| 131 | + |
| 132 | + public List<Alumno> findAlumnoEntities(int maxResults, int firstResult) { |
| 133 | + return findAlumnoEntities(false, maxResults, firstResult); |
| 134 | + } |
| 135 | + |
| 136 | + private List<Alumno> findAlumnoEntities(boolean all, int maxResults, int firstResult) { |
| 137 | + EntityManager em = getEntityManager(); |
| 138 | + try { |
| 139 | + CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); |
| 140 | + cq.select(cq.from(Alumno.class)); |
| 141 | + Query q = em.createQuery(cq); |
| 142 | + if (!all) { |
| 143 | + q.setMaxResults(maxResults); |
| 144 | + q.setFirstResult(firstResult); |
| 145 | + } |
| 146 | + return q.getResultList(); |
| 147 | + } finally { |
| 148 | + em.close(); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public Alumno findAlumno(int id) { |
| 153 | + EntityManager em = getEntityManager(); |
| 154 | + try { |
| 155 | + return em.find(Alumno.class, id); |
| 156 | + } finally { |
| 157 | + em.close(); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public int getAlumnoCount() { |
| 162 | + EntityManager em = getEntityManager(); |
| 163 | + try { |
| 164 | + CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); |
| 165 | + Root<Alumno> rt = cq.from(Alumno.class); |
| 166 | + cq.select(em.getCriteriaBuilder().count(rt)); |
| 167 | + Query q = em.createQuery(cq); |
| 168 | + return ((Long) q.getSingleResult()).intValue(); |
| 169 | + } finally { |
| 170 | + em.close(); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments