Skip to content
This repository was archived by the owner on Jan 30, 2019. It is now read-only.

Commit 7a39b85

Browse files
author
Arun Gupta
committed
New sample for optimistic concurrency locking
Former-commit-id: f5c0201a1dd3eefb04222ce4e7e38b35b772df0c
1 parent 0261e5d commit 7a39b85

9 files changed

Lines changed: 511 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<artifactId>jpa</artifactId>
6+
<groupId>org.sample</groupId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.glassfish.samples</groupId>
12+
<artifactId>locking-optimistic</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
16+
<name>locking-optimistic</name>
17+
</project>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.samples.locking.optimistic;
41+
42+
import java.io.Serializable;
43+
import javax.persistence.Entity;
44+
import javax.persistence.Id;
45+
import javax.persistence.NamedQueries;
46+
import javax.persistence.NamedQuery;
47+
import javax.persistence.Table;
48+
import javax.persistence.Version;
49+
import javax.validation.constraints.NotNull;
50+
import javax.validation.constraints.Size;
51+
import javax.xml.bind.annotation.XmlRootElement;
52+
53+
/**
54+
* @author Arun Gupta
55+
*/
56+
@Entity
57+
@Table(name = "MOVIE_LOCKING")
58+
@XmlRootElement
59+
@NamedQueries({
60+
@NamedQuery(name = "Movie.findAll", query = "SELECT m FROM Movie m"),
61+
@NamedQuery(name = "Movie.findById", query = "SELECT m FROM Movie m WHERE m.id = :id"),
62+
@NamedQuery(name = "Movie.findByName", query = "SELECT m FROM Movie m WHERE m.name = :name"),
63+
@NamedQuery(name = "Movie.findByActors", query = "SELECT m FROM Movie m WHERE m.actors = :actors")})
64+
public class Movie implements Serializable {
65+
private static final long serialVersionUID = 1L;
66+
@Id
67+
@NotNull
68+
private Integer id;
69+
70+
@Version int version;
71+
72+
@NotNull
73+
@Size(min = 1, max = 50)
74+
private String name;
75+
76+
@NotNull
77+
@Size(min = 1, max = 200)
78+
private String actors;
79+
80+
public Movie() {
81+
}
82+
83+
public Movie(Integer id) {
84+
this.id = id;
85+
}
86+
87+
public Movie(Integer id, String name, String actors) {
88+
this.id = id;
89+
this.name = name;
90+
this.actors = actors;
91+
}
92+
93+
public Integer getId() {
94+
return id;
95+
}
96+
97+
public void setId(Integer id) {
98+
this.id = id;
99+
}
100+
101+
public String getName() {
102+
return name;
103+
}
104+
105+
public void setName(String name) {
106+
this.name = name;
107+
}
108+
109+
public String getActors() {
110+
return actors;
111+
}
112+
113+
public void setActors(String actors) {
114+
this.actors = actors;
115+
}
116+
117+
public int getVersion() {
118+
return version;
119+
}
120+
121+
public void setVersion(int version) {
122+
this.version = version;
123+
}
124+
125+
@Override
126+
public String toString() {
127+
return name;
128+
}
129+
130+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.samples.locking.optimistic;
41+
42+
import java.util.List;
43+
import javax.ejb.Stateless;
44+
import javax.persistence.EntityManager;
45+
import javax.persistence.PersistenceContext;
46+
import javax.persistence.Query;
47+
import javax.persistence.TypedQuery;
48+
import javax.persistence.criteria.CriteriaBuilder;
49+
import javax.persistence.criteria.CriteriaDelete;
50+
import javax.persistence.criteria.CriteriaQuery;
51+
import javax.persistence.criteria.CriteriaUpdate;
52+
import javax.persistence.criteria.Root;
53+
54+
/**
55+
* @author Arun Gupta
56+
*/
57+
@Stateless
58+
public class MovieBean {
59+
60+
@PersistenceContext
61+
EntityManager em;
62+
63+
public List<Movie> listMovies() {
64+
CriteriaBuilder builder = em.getCriteriaBuilder();
65+
CriteriaQuery listCriteria = builder.createQuery(Movie.class);
66+
Root<Movie> listRoot = listCriteria.from(Movie.class);
67+
listCriteria.select(listRoot);
68+
TypedQuery<Movie> query = em.createQuery(listCriteria);
69+
return query.getResultList();
70+
}
71+
72+
public void updateMovie() {
73+
CriteriaBuilder builder = em.getCriteriaBuilder();
74+
CriteriaUpdate updateCriteria = builder.createCriteriaUpdate(Movie.class);
75+
Root<Movie> updateRoot = updateCriteria.from(Movie.class);
76+
updateCriteria.where(builder.equal(updateRoot.get(Movie_.name), "Inception"));
77+
updateCriteria.set(updateRoot.get(Movie_.name), "INCEPTION");
78+
Query q = em.createQuery(updateCriteria);
79+
q.executeUpdate();
80+
em.flush();
81+
}
82+
83+
public void deleteMovie() {
84+
CriteriaBuilder builder = em.getCriteriaBuilder();
85+
CriteriaDelete deleteCriteria = builder.createCriteriaDelete(Movie.class);
86+
Root<Movie> updateRoot = deleteCriteria.from(Movie.class);
87+
deleteCriteria.where(builder.equal(updateRoot.get(Movie_.name), "The Matrix"));
88+
Query q = em.createQuery(deleteCriteria);
89+
q.executeUpdate();
90+
em.flush();
91+
}
92+
93+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.samples.locking.optimistic;
41+
42+
import javax.persistence.metamodel.SingularAttribute;
43+
import javax.persistence.metamodel.StaticMetamodel;
44+
45+
/**
46+
* @author Arun Gupta
47+
*/
48+
@StaticMetamodel(Movie.class)
49+
public class Movie_ {
50+
51+
public static volatile SingularAttribute<Movie, Integer> id;
52+
public static volatile SingularAttribute<Movie, String> name;
53+
public static volatile SingularAttribute<Movie, String> actors;
54+
55+
}

0 commit comments

Comments
 (0)