Skip to content

Commit 5cbb204

Browse files
committed
Merge pull request #201 from kubamarchwicki/jpa-listeners-injection
JPA listener injection test
2 parents 36d26ab + 9085aad commit 5cbb204

File tree

12 files changed

+336
-0
lines changed

12 files changed

+336
-0
lines changed

jpa/listeners-injection/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.jpa</groupId>
6+
<artifactId>jpa-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>listeners-injection</artifactId>
12+
<packaging>war</packaging>
13+
<name>JPA Listeners Injection</name>
14+
<description>JPA 2.1 Entity Listeners injection</description>
15+
</project>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.javaee7.jpa.listeners;
2+
3+
import java.io.Serializable;
4+
import javax.persistence.Entity;
5+
import javax.persistence.EntityListeners;
6+
import javax.persistence.Id;
7+
import javax.persistence.NamedQueries;
8+
import javax.persistence.NamedQuery;
9+
import javax.persistence.Table;
10+
import javax.persistence.Transient;
11+
import javax.validation.constraints.NotNull;
12+
import javax.validation.constraints.Size;
13+
import javax.xml.bind.annotation.XmlRootElement;
14+
15+
/**
16+
* @author Arun Gupta
17+
*/
18+
@Entity
19+
@Table(name = "MOVIE_LISTENER")
20+
@XmlRootElement
21+
@NamedQueries({
22+
@NamedQuery(name = Movie.FIND_BY_NAME, query = "SELECT m FROM Movie m WHERE m.name = :name")
23+
})
24+
@EntityListeners(MovieListener.class)
25+
public class Movie implements Serializable {
26+
27+
private static final long serialVersionUID = 1L;
28+
public static final String FIND_BY_NAME = "Movie.findByName";
29+
@Id
30+
@NotNull
31+
private Integer id;
32+
33+
@NotNull
34+
@Size(min = 1, max = 50)
35+
private String name;
36+
37+
@NotNull
38+
@Size(min = 1, max = 200)
39+
private String actors;
40+
41+
@Transient
42+
private Integer rating;
43+
44+
public Movie() {
45+
}
46+
47+
public Movie(Integer id) {
48+
this.id = id;
49+
}
50+
51+
public Movie(Integer id, String name, String actors) {
52+
this.id = id;
53+
this.name = name;
54+
this.actors = actors;
55+
}
56+
57+
public Integer getId() {
58+
return id;
59+
}
60+
61+
public void setId(Integer id) {
62+
this.id = id;
63+
}
64+
65+
public String getName() {
66+
return name;
67+
}
68+
69+
public void setName(String name) {
70+
this.name = name;
71+
}
72+
73+
public String getActors() {
74+
return actors;
75+
}
76+
77+
public void setActors(String actors) {
78+
this.actors = actors;
79+
}
80+
81+
public Integer getRating() {
82+
return rating;
83+
}
84+
85+
public void setRating(Integer rating) {
86+
this.rating = rating;
87+
}
88+
89+
@Override
90+
public boolean equals(Object o) {
91+
if (this == o) { return true; }
92+
if (o == null || getClass() != o.getClass()) { return false; }
93+
94+
Movie movie = (Movie) o;
95+
96+
return id.equals(movie.id);
97+
}
98+
99+
@Override
100+
public int hashCode() {
101+
return id.hashCode();
102+
}
103+
104+
@Override
105+
public String toString() {
106+
return name;
107+
}
108+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.javaee7.jpa.listeners;
2+
3+
import javax.ejb.Stateless;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
7+
/**
8+
* @author Kuba Marchwicki
9+
*/
10+
@Stateless
11+
public class MovieBean {
12+
@PersistenceContext
13+
private EntityManager em;
14+
15+
public Movie getMovieByName(String name) {
16+
return em.createNamedQuery(Movie.FIND_BY_NAME, Movie.class)
17+
.setParameter("name", name)
18+
.getSingleResult();
19+
}
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.javaee7.jpa.listeners;
2+
3+
import javax.ejb.EJB;
4+
import javax.persistence.PostLoad;
5+
6+
/**
7+
* @author Kuba Marchwicki
8+
*/
9+
public class MovieListener {
10+
11+
@EJB
12+
RatingService service;
13+
14+
@PostLoad
15+
public void loadMovieRating(Movie movie) {
16+
Integer rating = service.movieRating(movie.getName());
17+
movie.setRating(rating);
18+
}
19+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package org.javaee7.jpa.listeners;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
import javax.persistence.NamedQueries;
6+
import javax.persistence.NamedQuery;
7+
import javax.persistence.Table;
8+
import javax.validation.constraints.NotNull;
9+
import javax.validation.constraints.Size;
10+
import java.io.Serializable;
11+
12+
/**
13+
* @author Kuba Marchwicki
14+
*/
15+
@Entity
16+
@Table(name = "MOVIE_RATINGS")
17+
@NamedQueries({
18+
@NamedQuery(name = Rating.FIND_BY_NAME, query = "SELECT r FROM Rating r WHERE r.name = :name")
19+
})
20+
public class Rating implements Serializable {
21+
22+
private static final long serialVersionUID = 1L;
23+
public static final String FIND_BY_NAME = "Rating.findByName";
24+
25+
@Id
26+
@NotNull
27+
private Integer id;
28+
29+
@NotNull
30+
@Size(min = 1, max = 50)
31+
private String name;
32+
33+
@NotNull
34+
private Integer rating;
35+
36+
public Integer getId() {
37+
return id;
38+
}
39+
40+
public void setId(Integer id) {
41+
this.id = id;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public void setName(String name) {
49+
this.name = name;
50+
}
51+
52+
public Integer getRating() {
53+
return rating;
54+
}
55+
56+
public void setRating(Integer rating) {
57+
this.rating = rating;
58+
}
59+
60+
@Override
61+
public boolean equals(Object o) {
62+
if (this == o) return true;
63+
if (!(o instanceof Rating)) return false;
64+
65+
Rating ratings = (Rating) o;
66+
67+
return id.equals(ratings.id);
68+
}
69+
70+
@Override
71+
public int hashCode() {
72+
return id.hashCode();
73+
}
74+
75+
@Override
76+
public String toString() {
77+
return name;
78+
}
79+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.javaee7.jpa.listeners;
2+
3+
import javax.ejb.Stateless;
4+
import javax.persistence.EntityManager;
5+
import javax.persistence.PersistenceContext;
6+
7+
/**
8+
* @author Kuba Marchwicki
9+
*/
10+
11+
@Stateless
12+
public class RatingService {
13+
@PersistenceContext
14+
private EntityManager em;
15+
16+
public Integer movieRating(String name) {
17+
return em.createNamedQuery(Rating.FIND_BY_NAME, Rating.class)
18+
.setParameter("name", name)
19+
.getSingleResult()
20+
.getRating();
21+
}
22+
23+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE MOVIE_LISTENER("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null, "ACTORS" VARCHAR(200) not null)
2+
CREATE TABLE MOVIE_RATINGS("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null, "RATING" INTEGER not null)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP TABLE MOVIE_LISTENER
2+
DROP TABLE MOVIE_RATINGS
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
INSERT INTO MOVIE_LISTENER("ID", "NAME", "ACTORS") VALUES (1, 'The Matrix', 'Keanu Reeves, Laurence Fishburne, Carrie-Ann Moss')
2+
INSERT INTO MOVIE_LISTENER("ID", "NAME", "ACTORS") VALUES (2, 'The Lord of The Rings', 'Elijah Wood, Ian Mckellen, Viggo Mortensen')
3+
INSERT INTO MOVIE_LISTENER("ID", "NAME", "ACTORS") VALUES (3, 'Inception', 'Leonardo DiCaprio')
4+
INSERT INTO MOVIE_LISTENER("ID", "NAME", "ACTORS") VALUES (4, 'The Shining', 'Jack Nicholson, Shelley Duvall')
5+
INSERT INTO MOVIE_RATINGS("ID", "NAME", "RATING") VALUES (1, 'The Matrix', 60)
6+
INSERT INTO MOVIE_RATINGS("ID", "NAME", "RATING") VALUES (2, 'The Lord of The Rings', 70)
7+
INSERT INTO MOVIE_RATINGS("ID", "NAME", "RATING") VALUES (3, 'Inception', 80)
8+
INSERT INTO MOVIE_RATINGS("ID", "NAME", "RATING") VALUES (4, 'The Shining', 90)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<persistence version="2.1"
3+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
6+
<persistence-unit name="myPU" transaction-type="JTA">
7+
<properties>
8+
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
9+
<property name="javax.persistence.schema-generation.create-source" value="script"/>
10+
<property name="javax.persistence.schema-generation.drop-source" value="script"/>
11+
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/>
12+
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop.sql"/>
13+
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
14+
</properties>
15+
</persistence-unit>
16+
</persistence>

0 commit comments

Comments
 (0)