Skip to content

Commit 7868a8c

Browse files
JPA Extended persistence context test
1 parent b4c080f commit 7868a8c

File tree

10 files changed

+118
-216
lines changed

10 files changed

+118
-216
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.javaee7.jpa.extended.pc;
2+
3+
import javax.enterprise.context.SessionScoped;
4+
import javax.inject.Inject;
5+
import java.io.Serializable;
6+
7+
@SessionScoped
8+
public class BigBangTheoryService implements Serializable {
9+
10+
@Inject
11+
CharactersBean characters;
12+
13+
public void addWilWheaton() {
14+
Character wil = new Character(8, "Wil Wheaton");
15+
characters.save(wil);
16+
}
17+
18+
public void updateRaj() {
19+
for (Character characterzz : characters.get()) {
20+
if ("Raj".equals(characterzz.getName())) {
21+
characterzz.setName("Rajesh Ramayan");
22+
characters.save(characterzz);
23+
}
24+
}
25+
}
26+
27+
public void proceed() {
28+
characters.commitChanges();
29+
}
30+
}

jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Employee.java renamed to jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/Character.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,42 @@
3939
*/
4040
package org.javaee7.jpa.extended.pc;
4141

42-
import java.io.Serializable;
4342
import javax.persistence.Column;
4443
import javax.persistence.Entity;
4544
import javax.persistence.Id;
4645
import javax.persistence.NamedQueries;
4746
import javax.persistence.NamedQuery;
4847
import javax.persistence.Table;
48+
import java.io.Serializable;
4949

5050
/**
5151
* @author Arun Gupta
5252
*/
5353
@Entity
54-
@Table(name="EMPLOYEE_SCHEMA_EXTENDED_PC")
54+
@Table(name="CHARACTERS")
5555
@NamedQueries({
56-
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e")
56+
@NamedQuery(name = Character.FIND_ALL, query = "SELECT c FROM Character c")
5757
})
58-
public class Employee implements Serializable {
58+
public class Character implements Serializable {
59+
60+
public static final String FIND_ALL = "Character.findAll";
61+
5962
private static final long serialVersionUID = 1L;
63+
6064
@Id
6165
private int id;
6266

6367
@Column(length=50)
6468
private String name;
6569

66-
public Employee() { }
70+
public Character() { }
6771

68-
public Employee(int id, String name) {
72+
public Character(int id, String name) {
6973
this.id = id;
7074
this.name = name;
7175
}
7276

73-
public Employee(String name) {
77+
public Character(String name) {
7478
this.name = name;
7579
}
7680

jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/EmployeeBean.java renamed to jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/CharactersBean.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,36 @@
3939
*/
4040
package org.javaee7.jpa.extended.pc;
4141

42-
import java.util.List;
4342
import javax.ejb.Stateful;
4443
import javax.ejb.TransactionAttribute;
4544
import javax.ejb.TransactionAttributeType;
4645
import javax.persistence.EntityManager;
4746
import javax.persistence.PersistenceContext;
4847
import javax.persistence.PersistenceContextType;
48+
import java.io.Serializable;
49+
import java.util.List;
4950

5051
/**
51-
* @author Arun Gupta
52+
* @author Kuba Marchwicki
5253
*/
5354
@Stateful
54-
public class EmployeeBean {
55+
@TransactionAttribute(TransactionAttributeType.NEVER)
56+
public class CharactersBean implements Serializable {
5557

5658
@PersistenceContext(type = PersistenceContextType.EXTENDED)
5759
EntityManager em;
5860

59-
@TransactionAttribute(TransactionAttributeType.NEVER)
60-
public void persistWithoutTransaction(Employee e) {
61+
public void save(Character e) {
6162
em.persist(e);
6263
}
6364

6465
@TransactionAttribute(TransactionAttributeType.REQUIRED)
65-
public void persistWithTransaction(Employee e) {
66-
em.persist(e);
66+
public void commitChanges() {
67+
6768
}
6869

69-
public List<Employee> get() {
70-
return em.createNamedQuery("Employee.findAll", Employee.class).getResultList();
70+
public List<Character> get() {
71+
return em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList();
7172
}
73+
7274
}

jpa/extended-pc/src/main/java/org/javaee7/jpa/extended/pc/TestServlet.java

Lines changed: 0 additions & 134 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CREATE TABLE EMPLOYEE_SCHEMA_EXTENDED_PC ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null)
1+
CREATE TABLE CHARACTERS ("ID" INTEGER not null primary key, "NAME" VARCHAR(50) not null)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
DROP TABLE EMPLOYEE_SCHEMA_EXTENDED_PC
1+
DROP TABLE CHARACTERS
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (1, 'Penny')
2-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (2, 'Sheldon')
3-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (3, 'Amy')
4-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (4, 'Leonard')
5-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (5, 'Bernadette')
6-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (6, 'Raj')
7-
INSERT INTO EMPLOYEE_SCHEMA_EXTENDED_PC("ID", "NAME") VALUES (7, 'Howard')
1+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (1, 'Penny')
2+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (2, 'Sheldon')
3+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (3, 'Amy')
4+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (4, 'Leonard')
5+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (5, 'Bernadette')
6+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (6, 'Raj')
7+
INSERT INTO CHARACTERS("ID", "NAME") VALUES (7, 'Howard')

jpa/extended-pc/src/main/resources/META-INF/persistence.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/>
1313
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop.sql"/>
1414
<property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>
15-
<property name="eclipselink.logging.level" value="FINE"/>
15+
<property name="eclipselink.logging.level" value="FINE"/>
1616
</properties>
1717
</persistence-unit>
1818
</persistence>

jpa/extended-pc/src/main/webapp/index.jsp

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

0 commit comments

Comments
 (0)