Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: dbremer-tonn/mentoring
base: aa128fb46b
...
head fork: dbremer-tonn/mentoring
compare: 2ef20c47b3
  • 2 commits
  • 22 files changed
  • 0 commit comments
  • 1 contributor
Showing with 970 additions and 181 deletions.
  1. +2 −1  jee5/mentoring-jee5-model/src/main/java/de/lbb/mentoring/example/model/Department.java
  2. +31 −8 jee5/mentoring-jee5-model/src/test/java/de/lbb/mentoring/example/model/RelationshipTest.java
  3. +40 −0 jee6/mentoring-jee6-model/pom.xml
  4. +26 −0 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/AbstractEntity.java
  5. +9 −13 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/Department.java
  6. +24 −14 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/Employee.java
  7. +40 −0 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/ParttimeEmployee.java
  8. +25 −0 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/valueobjects/EmployeeVO.java
  9. +23 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/AbstractEntityTest.java
  10. +0 −51 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/EmployeeTest.java
  11. +76 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/EqualityTest.java
  12. +46 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/InheritanceTest.java
  13. +39 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/QueryTest.java
  14. +96 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/RelationshipTest.java
  15. +119 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/TransactionTest.java
  16. +38 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa2/CacheTest.java
  17. +71 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa2/QueryTest.java
  18. +30 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/testdatabuilder/DepartmentBuilder.java
  19. +45 −0 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/testdatabuilder/EmployeeBuilder.java
  20. +41 −0 ...toring-jee6-model/src/test/java/de/lbb/mentoring/example/model/testdatabuilder/PartTimeEmployeeBuilder.java
  21. +10 −1 jee6/mentoring-jee6-model/src/test/resources/META-INF/persistence.xml
  22. +139 −93 jee6/pom.xml
3  jee5/mentoring-jee5-model/src/main/java/de/lbb/mentoring/example/model/Department.java
View
@@ -1,5 +1,6 @@
package de.lbb.mentoring.example.model;
+import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import java.util.List;
@@ -9,7 +10,7 @@
{
private String name;
- @OneToMany(mappedBy = "department")
+ @OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
private List<Employee> employees;
/**
39 jee5/mentoring-jee5-model/src/test/java/de/lbb/mentoring/example/model/RelationshipTest.java
View
@@ -6,6 +6,7 @@
import org.junit.Test;
import javax.persistence.EntityManager;
+import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;
@@ -13,7 +14,9 @@
{
/**
- * Bidrectional relationship: handling owning side successfully
+ * Bidrectional relationship:
+ * handling owning side successfully
+ * handling inverse side with failures
*/
@Test
public void testEmployeeDepartment()
@@ -31,11 +34,22 @@ public void testEmployeeDepartment()
em.persist(employee);
em.getTransaction().commit();
- // loading Employee from new EM
- EntityManager otherEM = emf.createEntityManager();
- Employee reloadedEmployee = otherEM.find(Employee.class, employee.getId());
+ // loading Employee
+ // employee is aware about his new Department
+ Employee reloadedEmployee = em.find(Employee.class, employee.getId());
Assert.assertNotNull(reloadedEmployee.getDepartment());
+
+ // Reloading the department
+ // department know nothing about its employees
+ Department reloadedDepartment = em.find(Department.class, department.getId()) ;
+ Assert.assertNull(reloadedDepartment.getEmployees());
+
+ // But loading same Department from different EM will work
+ EntityManager otherEM = emf.createEntityManager();
+ reloadedDepartment = otherEM.find(Department.class, department.getId()) ;
+ Assert.assertNotNull(reloadedDepartment.getEmployees());
+
}
/**
@@ -60,11 +74,20 @@ public void testDepartmentEmployees()
em.persist(department);
em.getTransaction().commit();
- // loading Department from new EM
+ // loading Department
+ Department reloadedDepartment = em.find(Department.class, department.getId());
+ Assert.assertNotNull(reloadedDepartment.getEmployees());
+ Assert.assertEquals(1, reloadedDepartment.getEmployees().size());
+ em.close();
+
+ // loading the Department from a new EM
EntityManager otherEM = emf.createEntityManager();
- Department reloadedDepartment = otherEM.find(Department.class, department.getId());
- // Employee-Relationship not maintained by jpa
- Assert.assertTrue("No employees expected!", reloadedDepartment.getEmployees().isEmpty());
+ reloadedDepartment = otherEM.find(Department.class, department.getId());
+ Assert.assertEquals(0, reloadedDepartment.getEmployees().size());
+
+ // ... but employees are persisted !
+ Query query = otherEM.createQuery("from Employee");
+ Assert.assertEquals(1, query.getResultList().size());
}
}
40 jee6/mentoring-jee6-model/pom.xml
View
@@ -13,7 +13,29 @@
<artifactId>mentoring-jee6-model</artifactId>
<name>Mentoring Model</name>
<version>1.0-SNAPSHOT</version>
+<!--
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>com.mysema.maven</groupId>
+ <artifactId>maven-apt-plugin</artifactId>
+ <version>1.0</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>process</goal>
+ </goals>
+ <configuration>
+ <outputDirectory>target/generated-sources/java</outputDirectory>
+ <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+-->
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
@@ -24,6 +46,24 @@
<artifactId>slf4j-log4j12</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-jpamodelgen</artifactId>
+ </dependency>
+
+ <!--<dependency>-->
+ <!--<groupId>org.hibernate</groupId>-->
+ <!--<artifactId>hibernate-ehcache</artifactId>-->
+ <!--</dependency>-->
+ <!--<dependency>-->
+ <!--<groupId>net.sf.ehcache</groupId>-->
+ <!--<artifactId>ehcache</artifactId>-->
+ <!--<type>pom</type>-->
+ <!--</dependency>-->
+ <!--<dependency>-->
+ <!--<groupId>net.sf.ehcache</groupId>-->
+ <!--<artifactId>ehcache-core</artifactId>-->
+ <!--</dependency>-->
<dependency>
<groupId>mysql</groupId>
26 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/AbstractEntity.java
View
@@ -0,0 +1,26 @@
+package de.lbb.mentoring.example.model;
+
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Inheritance;
+import javax.persistence.InheritanceType;
+import javax.persistence.MappedSuperclass;
+import javax.persistence.Version;
+import java.io.Serializable;
+
+@MappedSuperclass
+@Inheritance(strategy = InheritanceType.JOINED)
+public class AbstractEntity implements Serializable
+{
+ @Id
+ @GeneratedValue(strategy = GenerationType.TABLE) // Skipping Table strategy makes isolationlevel test on hsqldb fail
+ private Long id;
+
+ @Version
+ private Long version;
+
+ public Long getId() {
+ return id;
+ }
+}
22 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/Department.java
View
@@ -1,26 +1,22 @@
package de.lbb.mentoring.example.model;
-import javax.persistence.*;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.OneToMany;
import java.util.List;
@Entity
-public class Department
+public class Department extends AbstractEntity
{
- @Id
- @GeneratedValue
- private Long id;
-
private String name;
- @OneToMany
+ @OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
private List<Employee> employees;
- @Version
- private Long version;
-
- public Long getId() {
- return id;
- }
+ /**
+ * to be JPA conform
+ */
+ public Department() {}
public String getName() {
return name;
38 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/Employee.java
View
@@ -1,23 +1,38 @@
package de.lbb.mentoring.example.model;
-import javax.persistence.*;
+import javax.persistence.Cacheable;
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.ManyToOne;
+/**
+ * Requirements for a JPA-Entity from the spec:
+ *
+ * The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an
+ * entity.
+ * The entity class must have a no-arg constructor. The entity class may have other constructors as well.
+ * The no-arg constructor must be public or protected.
+ * The entity class must be a top-level class. An enum or interface should not be designated as an entity.
+ * The entity class must not be final. No methods or persistent instance variables of the entity class may be
+ * final.
+ * If an entity instance is to be passed by value as a detached object (e.g., through a remote interface), the
+ * entity class must implement the Serializable interface.
+ */
@Entity
-public class Employee
+@Cacheable(true)
+public class Employee extends AbstractEntity
{
- @Id
- @GeneratedValue
- private Long id;
-
private String name;
private String surename;
private int age;
- @ManyToOne
+ @ManyToOne(optional = true, cascade = CascadeType.ALL)
private Department department;
- @Version
- private Long version;
+ /**
+ * to be JPA conform
+ */
+ public Employee() {}
public String getName() {
return name;
@@ -50,9 +65,4 @@ public Department getDepartment() {
public void setDepartment(Department department) {
this.department = department;
}
-
- public Long getId() {
- return id;
- }
-
}
40 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/ParttimeEmployee.java
View
@@ -0,0 +1,40 @@
+package de.lbb.mentoring.example.model;
+
+
+import de.lbb.mentoring.example.model.Employee;
+
+import javax.persistence.Entity;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import java.util.Date;
+
+@Entity
+public class ParttimeEmployee extends Employee
+{
+ @Temporal(value = TemporalType.DATE)
+ // Try propertyname of 'from'
+ private Date fromDate;
+ @Temporal(value = TemporalType.DATE)
+ private Date toDate;
+
+ /**
+ * to be JPA conform
+ */
+ public ParttimeEmployee() {}
+
+ public Date getFrom() {
+ return fromDate;
+ }
+
+ public void setFrom(Date from) {
+ this.fromDate = from;
+ }
+
+ public Date getTo() {
+ return toDate;
+ }
+
+ public void setTo(Date to) {
+ this.toDate = to;
+ }
+}
25 jee6/mentoring-jee6-model/src/main/java/de/lbb/mentoring/example/model/valueobjects/EmployeeVO.java
View
@@ -0,0 +1,25 @@
+package de.lbb.mentoring.example.model.valueobjects;
+
+/**
+ * A ValueObject for the Employee
+ */
+public class EmployeeVO
+{
+ private String name;
+ private String surename;
+
+ public EmployeeVO(String name, String surename)
+ {
+ this.name = name;
+ this.surename = surename;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getSurename() {
+ return surename;
+ }
+
+}
23 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/AbstractEntityTest.java
View
@@ -0,0 +1,23 @@
+package de.lbb.mentoring.example.model;
+
+import org.junit.Before;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+public class AbstractEntityTest
+{
+
+ protected EntityManagerFactory emf;
+ protected EntityManager em;
+
+ @Before
+ public void setup()
+ {
+// emf = Persistence.createEntityManagerFactory("DataModel_MYSQL");
+ emf = Persistence.createEntityManagerFactory("DataModel");
+ em = emf.createEntityManager();
+ }
+
+}
51 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/EmployeeTest.java
View
@@ -1,51 +0,0 @@
-package de.lbb.mentoring.example.model;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.Persistence;
-
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-
-public class EmployeeTest
-{
-
- private EntityManagerFactory emf;
- private EntityManager em;
-
- @Before
- public void setup()
- {
- emf = Persistence.createEntityManagerFactory("DataModel");
- em = emf.createEntityManager();
- }
-
- @Test
- public void testEmployeeIdentity()
- {
- Employee employee = new Employee();
- employee.setName("Hans");
- employee.setSurename("Müller");
-
- // saving Employee
- em.getTransaction().begin();
- em.persist(employee);
- em.getTransaction().commit();
-
- // loading Employee from same EM
- Employee reloadedEmployee = em.find(Employee.class, employee.getId());
- Assert.assertTrue(employee == reloadedEmployee);
- Assert.assertTrue(employee.equals(reloadedEmployee));
-
- // loading Employee from new EM
- EntityManager otherEM = emf.createEntityManager();
- reloadedEmployee = otherEM.find(Employee.class, employee.getId());
- Assert.assertFalse(employee == reloadedEmployee);
- Assert.assertFalse(employee.equals(reloadedEmployee));
-
- // Implementing .equals and .hashcode to get equality again ...
- }
-
-
-}
76 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/EqualityTest.java
View
@@ -0,0 +1,76 @@
+package de.lbb.mentoring.example.model.jpa1;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+
+public class EqualityTest extends AbstractEntityTest
+{
+
+ /**
+ * Showing equality problem when loading same entity from different em
+ */
+ @Test
+ public void testEmployeeIdentity()
+ {
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // loading Employee from same EM
+ Employee reloadedEmployee = em.find(Employee.class, employee.getId());
+ Assert.assertTrue(employee == reloadedEmployee);
+ Assert.assertTrue(employee.equals(reloadedEmployee));
+
+ // loading Employee from new EM
+ EntityManager otherEM = emf.createEntityManager();
+ reloadedEmployee = otherEM.find(Employee.class, employee.getId());
+ Assert.assertFalse(employee == reloadedEmployee);
+ Assert.assertFalse(employee.equals(reloadedEmployee));
+
+ // Implementing .equals and .hashcode to get equality again ...
+ }
+
+ /**
+ * Showing equality problem when loading same entity from different em
+ */
+ @Test
+ public void testEmployeeAttachDetachment()
+ {
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // loading Employee from same EM
+ Employee employeeFromEM = em.find(Employee.class, employee.getId());
+ Assert.assertTrue(em.contains(employeeFromEM));
+
+ // loading Employee from new EM
+ EntityManager em2 = emf.createEntityManager();
+ Employee employeeFromEM2 = em2.find(Employee.class, employee.getId());
+ Assert.assertTrue(em2.contains(employeeFromEM2));
+
+ // no cross-attachments to the ems
+ Assert.assertFalse(em.contains(employeeFromEM2));
+ Assert.assertFalse(em2.contains(employeeFromEM));
+
+ // manual merging
+ Employee mergedEmployeeFromEM2 = em.merge(employeeFromEM2);
+ // merge is returning the attached entity !!!
+ Assert.assertTrue(mergedEmployeeFromEM2 != employeeFromEM2);
+
+ // equality of merged entities (same entity from the same EM must be the same instance!)
+ Assert.assertTrue(employeeFromEM == mergedEmployeeFromEM2);
+ }
+
+}
46 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/InheritanceTest.java
View
@@ -0,0 +1,46 @@
+package de.lbb.mentoring.example.model.jpa1;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.ParttimeEmployee;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.PartTimeEmployeeBuilder;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.Query;
+
+public class InheritanceTest extends AbstractEntityTest
+{
+
+ /**
+ * Showing equality problem when loading same entity from different em
+ */
+ @Test
+ public void testEmployeeInheritance()
+ {
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ Employee parttimeEmployee = new PartTimeEmployeeBuilder().withAge(25).withName("Peter").withSurename("Schneider").build();
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(parttimeEmployee);
+ em.getTransaction().commit();
+
+ // loading Employee from same EM
+ Employee reloadedEmployee = em.find(Employee.class, parttimeEmployee.getId());
+ Assert.assertTrue(reloadedEmployee instanceof ParttimeEmployee);
+
+ // loading via query
+ Query query = em.createQuery("from Employee where id = :id");
+ query.setParameter("id", parttimeEmployee.getId());
+ Employee result = (Employee) query.getSingleResult();
+ Assert.assertTrue(result instanceof ParttimeEmployee);
+ }
+}
39 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/QueryTest.java
View
@@ -0,0 +1,39 @@
+package de.lbb.mentoring.example.model.jpa1;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import de.lbb.mentoring.example.model.valueobjects.EmployeeVO;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+public class QueryTest extends AbstractEntityTest
+{
+
+ /**
+ * JPQL Query retrieving a valueobject directly
+ */
+ @Test
+ public void testEmployeeQueryValueObjects()
+ {
+ // Using a builder for Entities to test
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // loading Employee from new EM
+ EntityManager otherEM = emf.createEntityManager();
+ Query query = otherEM.createQuery("select new de.lbb.mentoring.example.model.valueobjects.EmployeeVO(e.name, e.surename) from Employee e where e.id = :id");
+ query.setParameter("id", employee.getId());
+ EmployeeVO emplVO = (EmployeeVO) query.getSingleResult();
+ Assert.assertEquals(employee.getName(), emplVO.getName());
+ Assert.assertEquals(employee.getSurename(), emplVO.getSurename());
+ }
+}
96 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/RelationshipTest.java
View
@@ -0,0 +1,96 @@
+package de.lbb.mentoring.example.model.jpa1;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Department;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.testdatabuilder.DepartmentBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.util.ArrayList;
+import java.util.List;
+
+public class RelationshipTest extends AbstractEntityTest
+{
+
+ /**
+ * Bidrectional relationship:
+ * handling owning side successfully
+ * handling inverse side with failures
+ */
+ @Test
+ public void testEmployeeDepartment()
+ {
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // Creating Department
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
+ employee.setDepartment(department);
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // loading Employee
+ // employee is aware about his new Department
+ Employee reloadedEmployee = em.find(Employee.class, employee.getId());
+ Assert.assertNotNull(reloadedEmployee.getDepartment());
+
+
+ // Reloading the department
+ // department know nothing about its employees
+ Department reloadedDepartment = em.find(Department.class, department.getId()) ;
+ Assert.assertNull(reloadedDepartment.getEmployees());
+
+ // But loading same Department from different EM will work
+ EntityManager otherEM = emf.createEntityManager();
+ reloadedDepartment = otherEM.find(Department.class, department.getId()) ;
+ Assert.assertNotNull(reloadedDepartment.getEmployees());
+
+ }
+
+ /**
+ * Bidrectional relationship: handling inverse side with failures
+ */
+ @Test
+ public void testDepartmentEmployees()
+ {
+ // Creating Department
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
+ // saving Department
+ em.getTransaction().begin();
+ em.persist(department);
+ em.getTransaction().commit();
+
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+ List<Employee> empls = new ArrayList<Employee>();
+ empls.add(employee);
+ department.setEmployees(empls);
+ em.getTransaction().begin();
+ em.persist(department);
+ em.getTransaction().commit();
+
+ // loading Department
+ Department reloadedDepartment = em.find(Department.class, department.getId());
+ Assert.assertNotNull(reloadedDepartment.getEmployees());
+ Assert.assertEquals(1, reloadedDepartment.getEmployees().size());
+ em.close();
+
+ // loading the Department from a new EM
+ EntityManager otherEM = emf.createEntityManager();
+ reloadedDepartment = otherEM.find(Department.class, department.getId());
+ Assert.assertEquals(0, reloadedDepartment.getEmployees().size());
+
+ // ... but employees are persisted !
+ Query query = otherEM.createQuery("from Employee");
+ Assert.assertEquals(1, query.getResultList().size());
+
+ }
+}
119 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa1/TransactionTest.java
View
@@ -0,0 +1,119 @@
+package de.lbb.mentoring.example.model.jpa1;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Department;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.testdatabuilder.DepartmentBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.EntityManager;
+import javax.persistence.FlushModeType;
+import javax.persistence.RollbackException;
+
+public class TransactionTest extends AbstractEntityTest
+{
+ /**
+ * Try with different ISOLATIONLEVELs ... have a look into persistence.xml under tests
+ * property: hibernate.connection.isolation
+ */
+ @Test
+ public void testIsolationLevel()
+ {
+Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
+ em.getTransaction().begin();
+
+ // From the spec:
+ // If FlushModeType.COMMIT is specified,
+ //flushing will occur at transaction commit; the persistence provider is permitted, but not required, to perform
+ //to flush at other times.
+ em.setFlushMode(FlushModeType.COMMIT);
+
+ employee.setDepartment(department);
+ em.persist(department);
+
+ // partial flush should be visible to same EM
+ Department reloadedDepartment = em.find(Department.class, department.getId());
+ Assert.assertNotNull(reloadedDepartment);
+
+ // check for flushed content in the db via second EM
+ // Will fail on hsqldb, because of lack of isolationlevel implementation
+ EntityManager em2 = emf.createEntityManager();
+ reloadedDepartment = em2.find(Department.class, department.getId());
+ Assert.assertNull("This one is failing if HSQLDB is used (here READ_UNCOMMITTED is actually used!) ...", reloadedDepartment);
+
+ em.getTransaction().commit();
+ }
+
+ /**
+ * Optimistic Locking expecting Exception
+ */
+ @Test(expected = RollbackException.class)
+ public void testOptimisticLocking()
+ {
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // fresh start
+ em = emf.createEntityManager();
+ EntityManager em2 = emf.createEntityManager();
+
+
+ Employee reloadedEmployeInstance1 = em.find(Employee.class, employee.getId());
+ Employee reloadedEmployeInstance2 = em2.find(Employee.class, employee.getId());
+
+ // --- client 1 ---
+ // saving employee1
+ reloadedEmployeInstance1.setAge(100);
+ em.getTransaction().begin();
+ em.persist(reloadedEmployeInstance1);
+ em.getTransaction().commit();
+
+ // --- client 2 ---
+ // saving employe2
+ reloadedEmployeInstance2.setAge(99);
+ em2.getTransaction().begin();
+ em2.persist(reloadedEmployeInstance2);
+ em2.getTransaction().commit();
+ }
+
+ /**
+ * PessimisticLocking: setting explizit lock and causing some deadlock
+ */
+ @Test
+ public void testPessimisticLocking()
+ {
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee);
+ em.getTransaction().commit();
+
+ // fresh start
+ em = emf.createEntityManager();
+ EntityManager em2 = emf.createEntityManager();
+
+ em.getTransaction().begin();
+ Employee reloadedEmployeInstance1 = em.find(Employee.class, employee.getId());
+
+ // Enable to get a deadlock
+// em.lock(reloadedEmployeInstance1, LockModeType.READ);
+
+ em2.getTransaction().begin();
+ Employee reloadedEmployeInstance2 = em2.find(Employee.class, employee.getId());
+ reloadedEmployeInstance2.setAge(99);
+ em2.persist(reloadedEmployeInstance2);
+ em2.getTransaction().commit();
+
+
+ em.getTransaction().commit();
+ }
+
+}
38 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa2/CacheTest.java
View
@@ -0,0 +1,38 @@
+package de.lbb.mentoring.example.model.jpa2;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.Cache;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+import java.util.List;
+
+public class CacheTest extends AbstractEntityTest
+{
+
+ /**
+ * JPA2 introduced typed queries
+ */
+ @Test
+ public void testSharedCache()
+ {
+ Employee employee1 = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+ Employee employee2 = new EmployeeBuilder().withAge(30).withName("Klaus").withSurename("Schulze").build();
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee1);
+ em.persist(employee2);
+ em.getTransaction().commit();
+
+ // Cache is not active by default
+ Cache cache = emf.getCache();
+ Assert.assertFalse(cache.contains(Employee.class, employee1));
+ Assert.assertFalse(cache.contains(Employee.class, employee2));
+ }
+}
71 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/jpa2/QueryTest.java
View
@@ -0,0 +1,71 @@
+package de.lbb.mentoring.example.model.jpa2;
+
+import de.lbb.mentoring.example.model.AbstractEntityTest;
+import de.lbb.mentoring.example.model.Employee;
+import de.lbb.mentoring.example.model.Employee_;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import org.hibernate.cfg.annotations.QueryBinder;
+import org.junit.Assert;
+import org.junit.Test;
+
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+import java.util.List;
+
+public class QueryTest extends AbstractEntityTest
+{
+
+ /**
+ * JPA2 introduced typed queries
+ */
+ @Test
+ public void testTypedQuery()
+ {
+ Employee employee1 = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+ Employee employee2 = new EmployeeBuilder().withAge(30).withName("Klaus").withSurename("Schulze").build();
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee1);
+ em.persist(employee2);
+ em.getTransaction().commit();
+
+ TypedQuery<Employee> query = em.createQuery("from Employee", Employee.class);
+ List<Employee> employees = query.getResultList();
+ Assert.assertEquals(2, employees.size());
+
+ }
+
+ /**
+ * Examples of the new and cool CriteriaBuilder
+ * Using the jpa2 Metamodel API
+ */
+ @Test
+ public void testCriteriaBuilder()
+ {
+ Employee employee1 = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+ Employee employee2 = new EmployeeBuilder().withAge(30).withName("Klaus").withSurename("Schulze").build();
+ // saving Employee
+ em.getTransaction().begin();
+ em.persist(employee1);
+ em.persist(employee2);
+ em.getTransaction().commit();
+
+ CriteriaBuilder cb = em.getCriteriaBuilder();
+
+ // building the query
+ CriteriaQuery<Employee> criteria = cb.createQuery(Employee.class);
+ Root<Employee> employeeRoot = criteria.from(Employee.class);
+
+ // using the jpa2 metamodel ..............
+ criteria.where(cb.equal(employeeRoot.get(Employee_.name), "Klaus"));
+
+ // running the query
+ TypedQuery<Employee> query = em.createQuery(criteria);
+ List<Employee> employees = query.getResultList();
+ Assert.assertEquals(1, employees.size());
+ Assert.assertEquals(employee2, query.getSingleResult());
+ }
+
+}
30 ...mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/testdatabuilder/DepartmentBuilder.java
View
@@ -0,0 +1,30 @@
+package de.lbb.mentoring.example.model.testdatabuilder;
+
+import de.lbb.mentoring.example.model.Department;
+
+
+public class DepartmentBuilder
+{
+ protected String name = null;
+ protected String surename = null;
+ protected int age = 0;
+
+ public DepartmentBuilder()
+ {
+ }
+
+ public DepartmentBuilder withName(String name)
+ {
+ this.name = name;
+ return this;
+ }
+
+ public Department build()
+ {
+ Department department = new Department();
+ if (this.name != null)
+ department.setName(this.name);
+
+ return department;
+ }
+}
45 jee6/mentoring-jee6-model/src/test/java/de/lbb/mentoring/example/model/testdatabuilder/EmployeeBuilder.java
View
@@ -0,0 +1,45 @@
+package de.lbb.mentoring.example.model.testdatabuilder;
+
+import de.lbb.mentoring.example.model.Employee;
+
+
+public class EmployeeBuilder
+{
+ protected String name = null;
+ protected String surename = null;
+ protected int age = 0;
+
+ public EmployeeBuilder()
+ {
+ }
+
+ public EmployeeBuilder withName(String name)
+ {
+ this.name = name;
+ return this;
+ }
+
+ public EmployeeBuilder withSurename(String surename)
+ {
+ this.surename = surename;
+ return this;
+ }
+
+ public EmployeeBuilder withAge(int age)
+ {
+ this.age = age;
+ return this;
+ }
+
+ public Employee build()
+ {
+ Employee employee = new Employee();
+ employee.setAge(this.age);
+ if (this.name != null)
+ employee.setName(this.name);
+ if (this.surename != null)
+ employee.setSurename(this.surename);
+
+ return employee;
+ }
+}
41 ...ing-jee6-model/src/test/java/de/lbb/mentoring/example/model/testdatabuilder/PartTimeEmployeeBuilder.java
View
@@ -0,0 +1,41 @@
+package de.lbb.mentoring.example.model.testdatabuilder;
+
+import de.lbb.mentoring.example.model.ParttimeEmployee;
+
+import java.util.Date;
+
+
+public class PartTimeEmployeeBuilder extends EmployeeBuilder
+{
+ protected Date fromDate = null;
+ protected Date toDate = null;
+
+
+ public PartTimeEmployeeBuilder()
+ {
+ }
+
+ public PartTimeEmployeeBuilder withFromDate(Date fromDate)
+ {
+ this.fromDate = fromDate;
+ return this;
+ }
+
+ public PartTimeEmployeeBuilder withToDate(Date toDate)
+ {
+ this.toDate = toDate;
+ return this;
+ }
+
+ public ParttimeEmployee build()
+ {
+ ParttimeEmployee employee = new ParttimeEmployee();
+ employee.setAge(this.age);
+ if (this.name != null)
+ employee.setName(this.name);
+ if (this.surename != null)
+ employee.setSurename(this.surename);
+
+ return employee;
+ }
+}
11 jee6/mentoring-jee6-model/src/test/resources/META-INF/persistence.xml
View
@@ -9,14 +9,16 @@
<persistence-unit name="DataModel_MYSQL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>de.lbb.mentoring.example.model.Employee</class>
+ <class>de.lbb.mentoring.example.model.ParttimeEmployee</class>
<class>de.lbb.mentoring.example.model.Department</class>
+ <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
-
+ <!--<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>-->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.connection.isolation" value="2"/>
@@ -27,8 +29,11 @@
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>de.lbb.mentoring.example.model.Employee</class>
+ <class>de.lbb.mentoring.example.model.ParttimeEmployee</class>
<class>de.lbb.mentoring.example.model.Department</class>
+ <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
+
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
@@ -44,8 +49,12 @@
<property name="hibernate.jdbc.batch_size" value="0"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
+ <!--<property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/>-->
+ <!--<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />-->
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
+
</properties>
+
</persistence-unit>
</persistence>
232 jee6/pom.xml
View
@@ -1,93 +1,139 @@
-<?xml version="1.0"?>
-<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/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
-
- <groupId>de.lbb.mentoring</groupId>
- <artifactId>mentoring-jee6</artifactId>
- <packaging>pom</packaging>
- <name>Mentoring Parent POM</name>
- <version>1.0-SNAPSHOT</version>
-
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-entitymanager</artifactId>
- <version>3.6.0.Final</version>
- <scope>provided</scope>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.16</version>
- <type>jar</type>
- <scope>test</scope>
- </dependency>
-
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.6.1</version>
- </dependency>
-
- <dependency>
- <groupId>hsqldb</groupId>
- <artifactId>hsqldb</artifactId>
- <version>1.8.0.10</version>
- <type>jar</type>
- <scope>test</scope>
- </dependency>
-
-<!--
- <dependency>
- <groupId>javax.enterprise</groupId>
- <artifactId>cdi-api</artifactId>
- <scope>provided</scope>
- <version>1.0-SP1</version>
- </dependency>
- <dependency>
- <groupId>javax.faces</groupId>
- <artifactId>jsf-api</artifactId>
- <scope>provided</scope>
- <version>2.0</version>
- </dependency>
-
- <dependency>
- <groupId>javax</groupId>
- <artifactId>javaee-web-api</artifactId>
- <version>6.0</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.validation</groupId>
- <artifactId>validation-api</artifactId>
- <version>1.0.0.GA</version>
- <scope>provided</scope>
- </dependency>
--->
- </dependencies>
- </dependencyManagement>
-
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>hsqldb</groupId>
- <artifactId>hsqldb</artifactId>
- </dependency>
-
- </dependencies>
-
- <modules>
- <module>mentoring-jee6-model</module>
-
- </modules>
-</project>
-
+<?xml version="1.0"?>
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>de.lbb.mentoring</groupId>
+ <artifactId>mentoring-jee6</artifactId>
+ <packaging>pom</packaging>
+ <name>Mentoring Parent POM</name>
+ <version>1.0-SNAPSHOT</version>
+
+ <properties>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <!--<compilerArguments>-->
+ <!--<processor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</processor>-->
+ <!--</compilerArguments>-->
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+
+ </build>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-entitymanager</artifactId>
+ <version>3.6.0.Final</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>mysql</groupId>
+ <artifactId>mysql-connector-java</artifactId>
+ <version>5.1.16</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-log4j12</artifactId>
+ <version>1.6.1</version>
+ </dependency>
+
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>1.8.0.10</version>
+ <type>jar</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate-jpamodelgen</artifactId>
+ <version>1.1.1.Final</version>
+ </dependency>
+
+ <!-- Hibernate 2nd level cache -->
+ <!--<dependency>-->
+ <!--<groupId>org.hibernate</groupId>-->
+ <!--<artifactId>hibernate-ehcache</artifactId>-->
+ <!--<version>3.6.3.Final</version>-->
+ <!--</dependency>-->
+ <!--<dependency>-->
+ <!--<groupId>net.sf.ehcache</groupId>-->
+ <!--<artifactId>ehcache</artifactId>-->
+ <!--<version>2.4.2</version>-->
+ <!--<type>pom</type>-->
+ <!--</dependency>-->
+ <!--<dependency>-->
+ <!--<groupId>net.sf.ehcache</groupId>-->
+ <!--<artifactId>ehcache-core</artifactId>-->
+ <!--<version>2.4.2</version>-->
+ <!--</dependency>-->
+
+ <!--
+ <dependency>
+ <groupId>javax.enterprise</groupId>
+ <artifactId>cdi-api</artifactId>
+ <scope>provided</scope>
+ <version>1.0-SP1</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.faces</groupId>
+ <artifactId>jsf-api</artifactId>
+ <scope>provided</scope>
+ <version>2.0</version>
+ </dependency>
+
+ <dependency>
+ <groupId>javax</groupId>
+ <artifactId>javaee-web-api</artifactId>
+ <version>6.0</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>javax.validation</groupId>
+ <artifactId>validation-api</artifactId>
+ <version>1.0.0.GA</version>
+ <scope>provided</scope>
+ </dependency>
+ -->
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ </dependency>
+
+ </dependencies>
+
+ <modules>
+ <module>mentoring-jee6-model</module>
+
+ </modules>
+</project>
+

No commit comments for this range

Something went wrong with that request. Please try again.