Skip to content

Commit d6d0474

Browse files
committed
Added Activity 17B Application - Unit 15
1 parent 7ac4bd7 commit d6d0474

File tree

6 files changed

+453
-0
lines changed

6 files changed

+453
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
package unit15.jpa.Actividad17B_Aplicacion;
3+
4+
import java.io.Serializable;
5+
import javax.persistence.Entity;
6+
import javax.persistence.GeneratedValue;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Articulo implements Serializable {
11+
@Id
12+
@GeneratedValue
13+
private Integer id;
14+
private String titulo;
15+
private Integer año;
16+
private Integer numPalabras;
17+
18+
public Articulo(String titulo, Integer año, Integer numPalabras) {
19+
this.titulo = titulo;
20+
this.año = año;
21+
this.numPalabras = numPalabras;
22+
}
23+
24+
public Articulo() {
25+
}
26+
27+
public Integer getId() {
28+
return id;
29+
}
30+
31+
public void setId(Integer id) {
32+
this.id = id;
33+
}
34+
35+
public String getTitulo() {
36+
return titulo;
37+
}
38+
39+
public void setTitulo(String titulo) {
40+
this.titulo = titulo;
41+
}
42+
43+
public Integer getAño() {
44+
return año;
45+
}
46+
47+
public void setAño(Integer año) {
48+
this.año = año;
49+
}
50+
51+
public Integer getNumPalabras() {
52+
return numPalabras;
53+
}
54+
55+
public void setNumPalabras(Integer numPalabras) {
56+
this.numPalabras = numPalabras;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "Articulo{" + "id=" + id + ", titulo=" + titulo +
62+
", a\u00f1o=" + año + ", numPalabras=" + numPalabras + '}';
63+
}
64+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
2+
package unit15.jpa.Actividad17B_Aplicacion;
3+
4+
import java.io.Serializable;
5+
import java.util.List;
6+
import javax.persistence.EntityManager;
7+
import javax.persistence.EntityManagerFactory;
8+
import javax.persistence.Query;
9+
import javax.persistence.EntityNotFoundException;
10+
import javax.persistence.criteria.CriteriaQuery;
11+
import javax.persistence.criteria.Root;
12+
import unit15.jpa.Actividad07.exceptions.NonexistentEntityException;
13+
14+
15+
public class ArticuloDAO implements Serializable {
16+
17+
public ArticuloDAO(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(Articulo articulo) {
27+
EntityManager em = null;
28+
try {
29+
em = getEntityManager();
30+
em.getTransaction().begin();
31+
em.persist(articulo);
32+
em.getTransaction().commit();
33+
} finally {
34+
if (em != null) {
35+
em.close();
36+
}
37+
}
38+
}
39+
40+
public void edit(Articulo articulo) throws NonexistentEntityException, Exception {
41+
EntityManager em = null;
42+
try {
43+
em = getEntityManager();
44+
em.getTransaction().begin();
45+
articulo = em.merge(articulo);
46+
em.getTransaction().commit();
47+
} catch (Exception ex) {
48+
String msg = ex.getLocalizedMessage();
49+
if (msg == null || msg.length() == 0) {
50+
Integer id = articulo.getId();
51+
if (findArticulo(id) == null) {
52+
throw new NonexistentEntityException("The articulo with id " + id + " no longer exists.");
53+
}
54+
}
55+
throw ex;
56+
} finally {
57+
if (em != null) {
58+
em.close();
59+
}
60+
}
61+
}
62+
63+
public void destroy(Integer id) throws NonexistentEntityException {
64+
EntityManager em = null;
65+
try {
66+
em = getEntityManager();
67+
em.getTransaction().begin();
68+
Articulo articulo;
69+
try {
70+
articulo = em.getReference(Articulo.class, id);
71+
articulo.getId();
72+
} catch (EntityNotFoundException enfe) {
73+
throw new NonexistentEntityException("The articulo with id " + id + " no longer exists.", enfe);
74+
}
75+
em.remove(articulo);
76+
em.getTransaction().commit();
77+
} finally {
78+
if (em != null) {
79+
em.close();
80+
}
81+
}
82+
}
83+
84+
public List<Articulo> findArticuloEntities() {
85+
return findArticuloEntities(true, -1, -1);
86+
}
87+
88+
public List<Articulo> findArticuloEntities(int maxResults, int firstResult) {
89+
return findArticuloEntities(false, maxResults, firstResult);
90+
}
91+
92+
private List<Articulo> findArticuloEntities(boolean all, int maxResults, int firstResult) {
93+
EntityManager em = getEntityManager();
94+
try {
95+
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
96+
cq.select(cq.from(Articulo.class));
97+
Query q = em.createQuery(cq);
98+
if (!all) {
99+
q.setMaxResults(maxResults);
100+
q.setFirstResult(firstResult);
101+
}
102+
return q.getResultList();
103+
} finally {
104+
em.close();
105+
}
106+
}
107+
108+
public Articulo findArticulo(Integer id) {
109+
EntityManager em = getEntityManager();
110+
try {
111+
return em.find(Articulo.class, id);
112+
} finally {
113+
em.close();
114+
}
115+
}
116+
117+
public int getArticuloCount() {
118+
EntityManager em = getEntityManager();
119+
try {
120+
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
121+
Root<Articulo> rt = cq.from(Articulo.class);
122+
cq.select(em.getCriteriaBuilder().count(rt));
123+
Query q = em.createQuery(cq);
124+
return ((Long) q.getSingleResult()).intValue();
125+
} finally {
126+
em.close();
127+
}
128+
}
129+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package unit15.jpa.Actividad17B_Aplicacion;
2+
3+
import java.util.List;
4+
import javax.persistence.EntityManagerFactory;
5+
import javax.persistence.Persistence;
6+
7+
8+
public class Main {
9+
10+
public static void main(String[] args) {
11+
EntityManagerFactory emf;
12+
emf = Persistence.createEntityManagerFactory("PeriodistasPU");
13+
14+
15+
PeriodistaDAO dao = new PeriodistaDAO(emf);
16+
17+
List<Periodista> todosPeriodistas = dao.findPeriodistaEntities();
18+
19+
20+
System.out.println("Periodistas sin artículos");
21+
for (Periodista p : todosPeriodistas) {
22+
if (p.getArticulos().isEmpty()) {
23+
System.out.println(p);
24+
}
25+
}
26+
27+
28+
29+
System.out.println("Periodistas con más de 2 artículos");
30+
for (Periodista p : todosPeriodistas) {
31+
if (p.getArticulos().size() > 2) {
32+
System.out.println(p);
33+
}
34+
}
35+
}
36+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package unit15.jpa.Actividad17B_Aplicacion;
2+
3+
import java.io.Serializable;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import javax.persistence.CascadeType;
7+
import javax.persistence.Entity;
8+
import javax.persistence.Id;
9+
import javax.persistence.OneToMany;
10+
11+
12+
@Entity
13+
public class Periodista implements Serializable {
14+
private String nombre;
15+
@Id
16+
private String dni;
17+
private String numTel;
18+
19+
20+
@OneToMany(cascade = CascadeType.ALL)
21+
private List<Articulo> articulos;
22+
23+
public Periodista(String nombre, String dni, String numTel) {
24+
this.nombre = nombre;
25+
this.dni = dni;
26+
this.numTel = numTel;
27+
this.articulos = new LinkedList<>();
28+
}
29+
30+
public Periodista() {
31+
}
32+
33+
34+
public void addArticulo(Articulo a) {
35+
articulos.add(a);
36+
}
37+
38+
public String getNombre() {
39+
return nombre;
40+
}
41+
42+
public void setNombre(String nombre) {
43+
this.nombre = nombre;
44+
}
45+
46+
public String getDni() {
47+
return dni;
48+
}
49+
50+
public void setDni(String dni) {
51+
this.dni = dni;
52+
}
53+
54+
public String getNumTel() {
55+
return numTel;
56+
}
57+
58+
public void setNumTel(String numTel) {
59+
this.numTel = numTel;
60+
}
61+
62+
63+
@Override
64+
65+
public String toString() {
66+
return "Periodista{" + "nombre=" + nombre + ", dni=" + dni + ", numTel=" + numTel + '}';
67+
}
68+
69+
public List<Articulo> getArticulos() {
70+
return articulos;
71+
}
72+
73+
public void setArticulos(List<Articulo> articulos) {
74+
this.articulos = articulos;
75+
}
76+
}

0 commit comments

Comments
 (0)