Skip to content

Commit 4ad88e9

Browse files
committed
Added tests for locking-optimistic project.
1 parent a4df3ab commit 4ad88e9

File tree

10 files changed

+269
-232
lines changed

10 files changed

+269
-232
lines changed

jpa/locking-optimistic/pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
34
<modelVersion>4.0.0</modelVersion>
45
<parent>
56
<groupId>org.javaee7.jpa</groupId>
@@ -8,8 +9,14 @@
89
<relativePath>../pom.xml</relativePath>
910
</parent>
1011

11-
<groupId>org.javaee7.jpa</groupId>
1212
<artifactId>locking-optimistic</artifactId>
1313
<version>1.0-SNAPSHOT</version>
1414
<packaging>war</packaging>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.jboss.shrinkwrap.descriptors</groupId>
19+
<artifactId>shrinkwrap-descriptors-impl-javaee</artifactId>
20+
</dependency>
21+
</dependencies>
1522
</project>
Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
11
package org.javaee7.jpa.locking.optimistic;
22

3-
import java.io.Serializable;
4-
import javax.persistence.Entity;
5-
import javax.persistence.Id;
6-
import javax.persistence.NamedQueries;
7-
import javax.persistence.NamedQuery;
8-
import javax.persistence.Table;
9-
import javax.persistence.Version;
3+
import javax.persistence.*;
104
import javax.validation.constraints.NotNull;
115
import javax.validation.constraints.Size;
12-
import javax.xml.bind.annotation.XmlRootElement;
6+
import java.io.Serializable;
137

148
/**
159
* @author Arun Gupta
1610
*/
1711
@Entity
1812
@Table(name = "MOVIE_OPTIMISTIC")
19-
@XmlRootElement
2013
@NamedQueries({
21-
@NamedQuery(name = "Movie.findAll", query = "SELECT m FROM Movie m"),
22-
@NamedQuery(name = "Movie.findById", query = "SELECT m FROM Movie m WHERE m.id = :id"),
23-
@NamedQuery(name = "Movie.findByName", query = "SELECT m FROM Movie m WHERE m.name = :name"),
24-
@NamedQuery(name = "Movie.findByActors", query = "SELECT m FROM Movie m WHERE m.actors = :actors")})
14+
@NamedQuery(name = "Movie.findAll", query = "SELECT m FROM Movie m"),
15+
})
2516
public class Movie implements Serializable {
26-
private static final long serialVersionUID = 1L;
2717
@Id
2818
@NotNull
2919
private Integer id;
3020

31-
@Version int version;
32-
21+
@Version
22+
private Integer version;
23+
3324
@NotNull
3425
@Size(min = 1, max = 50)
3526
private String name;
36-
27+
3728
@NotNull
3829
@Size(min = 1, max = 200)
3930
private String actors;
40-
41-
public Movie() {
42-
}
4331

44-
public Movie(Integer id) {
45-
this.id = id;
32+
public Integer getId() {
33+
return id;
4634
}
4735

48-
public Movie(Integer id, String name, String actors) {
36+
public void setId(Integer id) {
4937
this.id = id;
50-
this.name = name;
51-
this.actors = actors;
5238
}
5339

54-
public Integer getId() {
55-
return id;
40+
public Integer getVersion() {
41+
return version;
5642
}
5743

58-
public void setId(Integer id) {
59-
this.id = id;
44+
public void setVersion(Integer version) {
45+
this.version = version;
6046
}
6147

6248
public String getName() {
@@ -75,17 +61,35 @@ public void setActors(String actors) {
7561
this.actors = actors;
7662
}
7763

78-
public int getVersion() {
79-
return version;
64+
@Override
65+
public boolean equals(Object o) {
66+
if (this == o) { return true; }
67+
if (o == null || getClass() != o.getClass()) { return false; }
68+
69+
Movie movie = (Movie) o;
70+
71+
return id.equals(movie.id) && version.equals(movie.version);
8072
}
8173

82-
public void setVersion(int version) {
83-
this.version = version;
74+
@Override
75+
public int hashCode() {
76+
int result = id.hashCode();
77+
result = 31 * result + version.hashCode();
78+
return result;
8479
}
85-
80+
8681
@Override
8782
public String toString() {
88-
return name;
83+
return "Movie{id=" +
84+
id +
85+
", version=" +
86+
version +
87+
", name='" +
88+
name +
89+
'\'' +
90+
", actors='" +
91+
actors +
92+
'\'' +
93+
'}';
8994
}
90-
9195
}
Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
package org.javaee7.jpa.locking.optimistic;
22

3-
import java.util.List;
43
import javax.ejb.Stateless;
4+
import javax.ejb.TransactionAttribute;
5+
import javax.ejb.TransactionAttributeType;
56
import javax.persistence.EntityManager;
67
import javax.persistence.LockModeType;
78
import javax.persistence.PersistenceContext;
9+
import java.util.List;
810

911
/**
1012
* @author Arun Gupta
1113
*/
1214
@Stateless
1315
public class MovieBean {
14-
1516
@PersistenceContext
16-
EntityManager em;
17-
18-
public void addMovies() {
19-
Movie[] movies = new Movie[4];
20-
movies[0] = new Movie(1, "The Matrix", "Keanu Reeves, Laurence Fishburne, Carrie-Ann Moss");
21-
movies[1] = new Movie(2, "The Lord of The Rings", "Elijah Wood, Ian Mckellen, Viggo Mortensen");
22-
movies[2] = new Movie(3, "Inception", "Leonardo DiCaprio");
23-
movies[3] = new Movie(4, "The Shining", "Jack Nicholson, Shelley Duvall");
24-
for (Movie m : movies) {
25-
em.persist(m);
26-
}
27-
}
17+
private EntityManager em;
2818

2919
public List<Movie> listMovies() {
3020
return em.createNamedQuery("Movie.findAll", Movie.class).getResultList();
3121
}
3222

33-
public void updateMovie() {
34-
Movie m = em.find(Movie.class, 3);
35-
// em.lock(m, LockModeType.OPTIMISTIC);
36-
m.setName("INCEPTION");
37-
em.merge(m);
38-
em.flush();
23+
public Movie findMovie(Integer id) {
24+
return em.find(Movie.class, id);
3925
}
40-
41-
public void deleteMovie() {
42-
Movie m = em.find(Movie.class, 1);
43-
em.remove(m);
26+
27+
@TransactionAttribute(TransactionAttributeType.REQUIRED)
28+
public Movie readMovie(Integer id) {
29+
return em.find(Movie.class, id, LockModeType.OPTIMISTIC);
30+
}
31+
32+
@TransactionAttribute(TransactionAttributeType.REQUIRED)
33+
public void updateMovie(Integer id, String name) {
34+
Movie movie = findMovie(id);
35+
em.lock(movie, LockModeType.OPTIMISTIC);
36+
movie.setName(name);
37+
em.merge(movie);
4438
em.flush();
4539
}
46-
40+
41+
@TransactionAttribute(TransactionAttributeType.REQUIRED)
42+
public void deleteMovie(Integer id) {
43+
em.remove(findMovie(id));
44+
}
4745
}

jpa/locking-optimistic/src/main/java/org/javaee7/jpa/locking/optimistic/TestServlet.java

Lines changed: 0 additions & 104 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
INSERT INTO MOVIE_OPTIMISTIC("ID", "NAME", "ACTORS", "VERSION") VALUES (1, 'The Matrix', 'Keanu Reeves, Laurence Fishburne, Carrie-Ann Moss', 0)
2+
INSERT INTO MOVIE_OPTIMISTIC("ID", "NAME", "ACTORS", "VERSION") VALUES (2, 'The Lord of The Rings', 'Elijah Wood, Ian Mckellen, Viggo Mortensen', 0)
3+
INSERT INTO MOVIE_OPTIMISTIC("ID", "NAME", "ACTORS", "VERSION") VALUES (3, 'Inception', 'Leonardo DiCaprio', 0)
4+
INSERT INTO MOVIE_OPTIMISTIC("ID", "NAME", "ACTORS", "VERSION") VALUES (4, 'The Shining', 'Jack Nicholson, Shelley Duvall', 0)
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<persistence
3-
version="2.1"
4-
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<persistence
3+
version="2.1"
4+
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
77
<persistence-unit name="myPU" transaction-type="JTA">
88
<properties>
99
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
1010
<property name="javax.persistence.schema-generation.create-source" value="metadata"/>
11+
<property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
12+
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
1113
</properties>
1214
</persistence-unit>
1315
</persistence>

jpa/locking-optimistic/src/main/webapp/WEB-INF/beans.xml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)