diff --git a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Character.java b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Character.java new file mode 100644 index 000000000..9e7b6287d --- /dev/null +++ b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Character.java @@ -0,0 +1,57 @@ +package org.javaee7.jpa.extended.pc; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import java.io.Serializable; + +/** + * @author Arun Gupta + */ +@Entity +@Table(name="CHARACTERS") +@NamedQueries({ + @NamedQuery(name = Character.FIND_ALL, query = "SELECT c FROM Character c") +}) +public class Character implements Serializable { + + public static final String FIND_ALL = "Character.findAll"; + + private static final long serialVersionUID = 1L; + + @Id + private int id; + + @Column(length=50) + private String name; + + public Character() { } + + public Character(int id, String name) { + this.id = id; + this.name = name; + } + + public Character(String name) { + this.name = name; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/CharactersBean.java b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/CharactersBean.java new file mode 100644 index 000000000..7a3141874 --- /dev/null +++ b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/CharactersBean.java @@ -0,0 +1,35 @@ +package org.javaee7.jpa.extended.pc; + +import javax.ejb.Stateful; +import javax.ejb.TransactionAttribute; +import javax.ejb.TransactionAttributeType; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.PersistenceContextType; +import java.io.Serializable; +import java.util.List; + +/** + * @author Kuba Marchwicki + */ +@Stateful +@TransactionAttribute(TransactionAttributeType.NEVER) +public class CharactersBean implements Serializable { + + @PersistenceContext(type = PersistenceContextType.EXTENDED) + EntityManager em; + + public void save(Character e) { + em.persist(e); + } + + @TransactionAttribute(TransactionAttributeType.REQUIRED) + public void commitChanges() { + + } + + public List get() { + return em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList(); + } + +} diff --git a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Employee.java b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Employee.java deleted file mode 100644 index c1c4d4977..000000000 --- a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Employee.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can - * obtain a copy of the License at - * http://glassfish.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. - * - * GPL Classpath Exception: - * Oracle designates this particular file as subject to the "Classpath" - * exception as provided by Oracle in the GPL Version 2 section of the License - * file that accompanied this code. - * - * Modifications: - * If applicable, add the following below the License Header, with the fields - * enclosed by brackets [] replaced by your own identifying information: - * "Portions Copyright [year] [name of copyright owner]" - * - * Contributor(s): - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ -package org.javaee7.jpa.extended.pc; - -import java.io.Serializable; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.NamedQueries; -import javax.persistence.NamedQuery; -import javax.persistence.Table; - -/** - * @author Arun Gupta - */ -@Entity -@Table(name="EMPLOYEE_SCHEMA_EXTENDED_PC") -@NamedQueries({ - @NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e") -}) -public class Employee implements Serializable { - private static final long serialVersionUID = 1L; - @Id - private int id; - - @Column(length=50) - private String name; - - public Employee() { } - - public Employee(int id, String name) { - this.id = id; - this.name = name; - } - - public Employee(String name) { - this.name = name; - } - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/EmployeeBean.java b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/EmployeeBean.java deleted file mode 100644 index 70082c99e..000000000 --- a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/EmployeeBean.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can - * obtain a copy of the License at - * http://glassfish.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. - * - * GPL Classpath Exception: - * Oracle designates this particular file as subject to the "Classpath" - * exception as provided by Oracle in the GPL Version 2 section of the License - * file that accompanied this code. - * - * Modifications: - * If applicable, add the following below the License Header, with the fields - * enclosed by brackets [] replaced by your own identifying information: - * "Portions Copyright [year] [name of copyright owner]" - * - * Contributor(s): - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ -package org.javaee7.jpa.extended.pc; - -import java.util.List; -import javax.ejb.Stateful; -import javax.ejb.TransactionAttribute; -import javax.ejb.TransactionAttributeType; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import javax.persistence.PersistenceContextType; - -/** - * @author Arun Gupta - */ -@Stateful -public class EmployeeBean { - - @PersistenceContext(type = PersistenceContextType.EXTENDED) - EntityManager em; - - @TransactionAttribute(TransactionAttributeType.NEVER) - public void persistWithoutTransaction(Employee e) { - em.persist(e); - } - - @TransactionAttribute(TransactionAttributeType.REQUIRED) - public void persistWithTransaction(Employee e) { - em.persist(e); - } - - public List get() { - return em.createNamedQuery("Employee.findAll", Employee.class).getResultList(); - } -} diff --git a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/TestServlet.java b/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/TestServlet.java deleted file mode 100644 index a559cc1e2..000000000 --- a/jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/TestServlet.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. - * - * The contents of this file are subject to the terms of either the GNU - * General Public License Version 2 only ("GPL") or the Common Development - * and Distribution License("CDDL") (collectively, the "License"). You - * may not use this file except in compliance with the License. You can - * obtain a copy of the License at - * http://glassfish.java.net/public/CDDL+GPL_1_1.html - * or packager/legal/LICENSE.txt. See the License for the specific - * language governing permissions and limitations under the License. - * - * When distributing the software, include this License Header Notice in each - * file and include the License file at packager/legal/LICENSE.txt. - * - * GPL Classpath Exception: - * Oracle designates this particular file as subject to the "Classpath" - * exception as provided by Oracle in the GPL Version 2 section of the License - * file that accompanied this code. - * - * Modifications: - * If applicable, add the following below the License Header, with the fields - * enclosed by brackets [] replaced by your own identifying information: - * "Portions Copyright [year] [name of copyright owner]" - * - * Contributor(s): - * If you wish your version of this file to be governed by only the CDDL or - * only the GPL Version 2, indicate your decision by adding "[Contributor] - * elects to include this software in this distribution under the [CDDL or GPL - * Version 2] license." If you don't indicate a single choice of license, a - * recipient has the option to distribute your version of this file under - * either the CDDL, the GPL Version 2 or to extend the choice of license to - * its licensees as provided above. However, if you add GPL Version 2 code - * and therefore, elected the GPL Version 2 license, then the option applies - * only if the new code is made subject to such option by the copyright - * holder. - */ -package org.javaee7.jpa.extended.pc; - -import java.io.IOException; -import java.io.PrintWriter; -import javax.inject.Inject; -import javax.servlet.ServletException; -import javax.servlet.annotation.WebServlet; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -/** - * @author Arun Gupta - */ -@WebServlet(urlPatterns = {"/TestServlet"}) -public class TestServlet extends HttpServlet { - - @Inject - EmployeeBean bean; - - /** - * Processes requests for both HTTP GET and POST - * methods. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - protected void processRequest(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - response.setContentType("text/html;charset=UTF-8"); - PrintWriter out = response.getWriter(); - out.println(""); - out.println(""); - out.println("Extended Persistence Context"); - out.println(""); - out.println(""); - out.println("

Extended Persistence Context

"); - listEmployees(out, "Initial list", "Total == 7?"); - Employee e = new Employee(8, "Priya"); - bean.persistWithoutTransaction(e); - listEmployees(out, "PC without a transaction", "Total == 7?"); - bean.persistWithTransaction(e); - listEmployees(out, "PC with a transaction", "Total == 8?"); - out.println(""); - out.println(""); - } - - private void listEmployees(PrintWriter out, String title, String result) { - out.println("

" + title + " (" + bean.get().size() + ") " + result + "

"); - for (Employee e : bean.get()) { - out.println(e.getName() + "
"); - } - } - - // - /** - * Handles the HTTP GET method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Handles the HTTP POST method. - * - * @param request servlet request - * @param response servlet response - * @throws ServletException if a servlet-specific error occurs - * @throws IOException if an I/O error occurs - */ - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) - throws ServletException, IOException { - processRequest(request, response); - } - - /** - * Returns a short description of the servlet. - * - * @return a String containing servlet description - */ - @Override - public String getServletInfo() { - return "Short description"; - }// -} diff --git a/jpa/extended-pc/src/main/resources/META-INF/create.sql b/jpa/extended-pc/src/main/resources/META-INF/create.sql index 2b9dc3a28..fd3a09ffd 100644 --- a/jpa/extended-pc/src/main/resources/META-INF/create.sql +++ b/jpa/extended-pc/src/main/resources/META-INF/create.sql @@ -1 +1 @@ -CREATE TABLE EMPLOYEE_SCHEMA_EXTENDED_PC ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null) \ No newline at end of file +CREATE TABLE CHARACTERS ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null) \ No newline at end of file diff --git a/jpa/extended-pc/src/main/resources/META-INF/drop.sql b/jpa/extended-pc/src/main/resources/META-INF/drop.sql index a75c07a18..19438f4a2 100644 --- a/jpa/extended-pc/src/main/resources/META-INF/drop.sql +++ b/jpa/extended-pc/src/main/resources/META-INF/drop.sql @@ -1 +1 @@ -DROP TABLE EMPLOYEE_SCHEMA_EXTENDED_PC \ No newline at end of file +DROP TABLE CHARACTERS \ No newline at end of file diff --git a/jpa/extended-pc/src/main/resources/META-INF/load.sql b/jpa/extended-pc/src/main/resources/META-INF/load.sql index 44c6793d5..2488ad5cf 100644 --- a/jpa/extended-pc/src/main/resources/META-INF/load.sql +++ b/jpa/extended-pc/src/main/resources/META-INF/load.sql @@ -1,7 +1,7 @@ -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (1, 'Penny') -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (2, 'Sheldon') -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (3, 'Amy') -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (4, 'Leonard') -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (5, 'Bernadette') -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (6, 'Raj') -INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (7, 'Howard') \ No newline at end of file +INSERT INTO CHARACTERS("ID", "NAME") VALUES (1, 'Penny') +INSERT INTO CHARACTERS("ID", "NAME") VALUES (2, 'Sheldon') +INSERT INTO CHARACTERS("ID", "NAME") VALUES (3, 'Amy') +INSERT INTO CHARACTERS("ID", "NAME") VALUES (4, 'Leonard') +INSERT INTO CHARACTERS("ID", "NAME") VALUES (5, 'Bernadette') +INSERT INTO CHARACTERS("ID", "NAME") VALUES (6, 'Raj') +INSERT INTO CHARACTERS("ID", "NAME") VALUES (7, 'Howard') \ No newline at end of file diff --git a/jpa/extended-pc/src/main/resources/META-INF/persistence.xml b/jpa/extended-pc/src/main/resources/META-INF/persistence.xml index 010072bfd..34ed09061 100644 --- a/jpa/extended-pc/src/main/resources/META-INF/persistence.xml +++ b/jpa/extended-pc/src/main/resources/META-INF/persistence.xml @@ -12,7 +12,7 @@ - + diff --git a/jpa/extended-pc/src/main/webapp/index.jsp b/jpa/extended-pc/src/main/webapp/index.jsp deleted file mode 100644 index c5fab84e0..000000000 --- a/jpa/extended-pc/src/main/webapp/index.jsp +++ /dev/null @@ -1,56 +0,0 @@ - -<%@page contentType="text/html" pageEncoding="UTF-8"%> - - - - - - JPA 2.1 Extended Persistence Context - - -

JPA 2.1 Extended Persistence Context

- - Add & List employees. - - diff --git a/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy b/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy new file mode 100644 index 000000000..dd6f5a727 --- /dev/null +++ b/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy @@ -0,0 +1,63 @@ +package org.javaee7.jpa.extended.pc + +import org.jboss.arquillian.container.test.api.Deployment +import org.jboss.arquillian.spock.ArquillianSputnik +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.WebArchive +import org.junit.runner.RunWith +import spock.lang.Specification + +import javax.ejb.EJB +import javax.persistence.EntityManager +import javax.persistence.PersistenceContext + +/** + * @author Kuba Marchwicki + */ +@RunWith(ArquillianSputnik) +class ExtendedPersistenceContextSpecification extends Specification { + + @PersistenceContext + EntityManager em; + + @EJB + CharactersBean bean; + + @Deployment + def static WebArchive deploy() { + ShrinkWrap.create(WebArchive.class) + .addPackage("org.javaee7.jpa.extended.pc") + .addAsResource("META-INF/persistence.xml") + .addAsResource("META-INF/create.sql") + .addAsResource("META-INF/drop.sql") + .addAsResource("META-INF/load.sql") + } + + def setup() { + Character wil = new Character(8, "Wil Wheaton") + bean.save(wil) + + for (Character c : bean.get()) { + if ("Raj".equals(c.getName())) { + c.setName("Rajesh Ramayan") + bean.save(c) + } + } + } + + def "should not persist changes without transaction flush"() { + expect: + 7 == em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList().size(); + "Raj" == em.find(Character.class, 6).name + } + + def "should update characters after transaction flush"() { + when: + bean.commitChanges() + + then: + 8 == em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList().size(); + "Rajesh Ramayan" == em.find(Character.class, 6).name + "Wil Wheaton" == em.find(Character.class, 8).name + } +}