Skip to content

Commit 0ad2124

Browse files
committed
Added Activity 16 Application - Unit 15
1 parent b8384b2 commit 0ad2124

File tree

6 files changed

+695
-0
lines changed

6 files changed

+695
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package unit15.jpa.Actividad16_Aplicacion;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.Id;
6+
import javax.persistence.OneToOne;
7+
8+
9+
@Entity
10+
public class Alumno implements Serializable {
11+
@Id
12+
private int num;
13+
private String nombre;
14+
private String tlf;
15+
16+
@OneToOne(orphanRemoval = false)
17+
private Portatil portatil;
18+
19+
20+
public Alumno(int num, String nombre, String tlf, Portatil portatil) {
21+
this.num = num;
22+
this.nombre = nombre;
23+
this.tlf = tlf;
24+
this.portatil = portatil;
25+
}
26+
27+
28+
public Alumno() {
29+
}
30+
31+
public int getNum() {
32+
return num;
33+
}
34+
35+
public void setNum(int num) {
36+
this.num = num;
37+
}
38+
39+
public String getNombre() {
40+
return nombre;
41+
}
42+
43+
public void setNombre(String nombre) {
44+
this.nombre = nombre;
45+
}
46+
47+
public String getTlf() {
48+
return tlf;
49+
}
50+
51+
public void setTlf(String tlf) {
52+
this.tlf = tlf;
53+
}
54+
55+
public Portatil getPortatil() {
56+
return portatil;
57+
}
58+
59+
public void setPortatil(Portatil portatil) {
60+
this.portatil = portatil;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Alumno{" + "num=" + num + ", nombre=" + nombre +
66+
", tlf=" + tlf + ", portatil=" + portatil + '}';
67+
}
68+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy