Skip to content

Commit 93398a2

Browse files
committed
Added Activity 18 Application - Unit 15
1 parent d6d0474 commit 93398a2

File tree

6 files changed

+620
-0
lines changed

6 files changed

+620
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package unit15.jpa.Actividad18_Aplicacion;
2+
3+
import java.io.Serializable;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
import javax.persistence.OneToMany;
10+
11+
12+
@Entity
13+
public class Equipo implements Serializable {
14+
@Id
15+
@GeneratedValue
16+
private int nie;
17+
private String nombre;
18+
19+
@OneToMany(orphanRemoval = false)
20+
private List<Jugador> jugadores;
21+
22+
public Equipo(String nombre) {
23+
this.nombre = nombre;
24+
25+
this.jugadores = new ArrayList<>();
26+
}
27+
28+
public Equipo() {
29+
30+
}
31+
32+
33+
34+
public void addJugador(Jugador j) {
35+
jugadores.add(j);
36+
}
37+
38+
39+
public void quitaJugador(Jugador j) {
40+
jugadores.remove(j);
41+
}
42+
43+
public String getNombre() {
44+
return nombre;
45+
}
46+
47+
public void setNombre(String nombre) {
48+
this.nombre = nombre;
49+
}
50+
51+
public int getNie() {
52+
return nie;
53+
}
54+
55+
public void setNie(int nie) {
56+
this.nie = nie;
57+
}
58+
59+
60+
public List<Jugador> getJugadores() {
61+
return jugadores;
62+
}
63+
64+
public void setJugadores(List<Jugador> jugadores) {
65+
this.jugadores = jugadores;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return "Equipo{" + "nie=" + nie + ", nombre=" + nombre + '}';
71+
}
72+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package unit15.jpa.Actividad18_Aplicacion;
2+
3+
import unit15.jpa.Actividad07.exceptions.NonexistentEntityException;
4+
5+
import java.io.Serializable;
6+
import java.util.List;
7+
import javax.persistence.EntityManager;
8+
import javax.persistence.EntityManagerFactory;
9+
import javax.persistence.Query;
10+
import javax.persistence.EntityNotFoundException;
11+
import javax.persistence.criteria.CriteriaQuery;
12+
import javax.persistence.criteria.Root;
13+
14+
public class EquipoDAO implements Serializable {
15+
16+
public EquipoDAO(EntityManagerFactory emf) {
17+
this.emf = emf;
18+
}
19+
private EntityManagerFactory emf = null;
20+
21+
public EntityManager getEntityManager() {
22+
return emf.createEntityManager();
23+
}
24+
25+
public void create(Equipo equipo) {
26+
EntityManager em = null;
27+
try {
28+
em = getEntityManager();
29+
em.getTransaction().begin();
30+
em.persist(equipo);
31+
em.getTransaction().commit();
32+
} finally {
33+
if (em != null) {
34+
em.close();
35+
}
36+
}
37+
}
38+
39+
public void edit(Equipo equipo) throws NonexistentEntityException, Exception {
40+
EntityManager em = null;
41+
try {
42+
em = getEntityManager();
43+
em.getTransaction().begin();
44+
equipo = em.merge(equipo);
45+
em.getTransaction().commit();
46+
} catch (Exception ex) {
47+
String msg = ex.getLocalizedMessage();
48+
if (msg == null || msg.length() == 0) {
49+
int id = equipo.getNie();
50+
if (findEquipo(id) == null) {
51+
throw new NonexistentEntityException("The equipo with id " + id + " no longer exists.");
52+
}
53+
}
54+
throw ex;
55+
} finally {
56+
if (em != null) {
57+
em.close();
58+
}
59+
}
60+
}
61+
62+
public void destroy(int id) throws NonexistentEntityException {
63+
EntityManager em = null;
64+
try {
65+
em = getEntityManager();
66+
em.getTransaction().begin();
67+
Equipo equipo;
68+
try {
69+
equipo = em.getReference(Equipo.class, id);
70+
equipo.getNie();
71+
} catch (EntityNotFoundException enfe) {
72+
throw new NonexistentEntityException("The equipo with id " + id + " no longer exists.", enfe);
73+
}
74+
em.remove(equipo);
75+
em.getTransaction().commit();
76+
} finally {
77+
if (em != null) {
78+
em.close();
79+
}
80+
}
81+
}
82+
83+
public List<Equipo> findEquipoEntities() {
84+
return findEquipoEntities(true, -1, -1);
85+
}
86+
87+
public List<Equipo> findEquipoEntities(int maxResults, int firstResult) {
88+
return findEquipoEntities(false, maxResults, firstResult);
89+
}
90+
91+
private List<Equipo> findEquipoEntities(boolean all, int maxResults, int firstResult) {
92+
EntityManager em = getEntityManager();
93+
try {
94+
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
95+
cq.select(cq.from(Equipo.class));
96+
Query q = em.createQuery(cq);
97+
if (!all) {
98+
q.setMaxResults(maxResults);
99+
q.setFirstResult(firstResult);
100+
}
101+
return q.getResultList();
102+
} finally {
103+
em.close();
104+
}
105+
}
106+
107+
public Equipo findEquipo(int id) {
108+
EntityManager em = getEntityManager();
109+
try {
110+
return em.find(Equipo.class, id);
111+
} finally {
112+
em.close();
113+
}
114+
}
115+
116+
public int getEquipoCount() {
117+
EntityManager em = getEntityManager();
118+
try {
119+
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
120+
Root<Equipo> rt = cq.from(Equipo.class);
121+
cq.select(em.getCriteriaBuilder().count(rt));
122+
Query q = em.createQuery(cq);
123+
return ((Long) q.getSingleResult()).intValue();
124+
} finally {
125+
em.close();
126+
}
127+
}
128+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
package unit15.jpa.Actividad18_Aplicacion;
3+
4+
import java.io.Serializable;
5+
import java.util.Objects;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
10+
11+
@Entity
12+
public class Jugador implements Serializable {
13+
@Id
14+
@GeneratedValue
15+
private Integer nij;
16+
private String nombre;
17+
private Integer puntuacionMax;
18+
19+
public Jugador(String nombre, Integer puntuacionMax) {
20+
this.nombre = nombre;
21+
this.puntuacionMax = puntuacionMax;
22+
}
23+
24+
public Jugador() {
25+
}
26+
27+
28+
public Integer getNij() {
29+
return nij;
30+
}
31+
32+
public void setNij(Integer nij) {
33+
this.nij = nij;
34+
}
35+
36+
public String getNombre() {
37+
return nombre;
38+
}
39+
40+
public void setNombre(String nombre) {
41+
this.nombre = nombre;
42+
}
43+
44+
public Integer getPuntuacionMax() {
45+
return puntuacionMax;
46+
}
47+
48+
public void setPuntuacionMax(Integer puntuacionMax) {
49+
this.puntuacionMax = puntuacionMax;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return "Jugador{" + "nij=" + nij + ", nombre=" + nombre +
55+
", puntuacionMax=" + puntuacionMax + '}';
56+
}
57+
58+
@Override
59+
public boolean equals(Object obj) {
60+
if (this == obj) {
61+
return true;
62+
}
63+
if (obj == null) {
64+
return false;
65+
}
66+
if (getClass() != obj.getClass()) {
67+
return false;
68+
}
69+
final Jugador other = (Jugador) obj;
70+
if (!Objects.equals(this.nij, other.nij)) {
71+
return false;
72+
}
73+
return true;
74+
}
75+
}

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