[jboss-cvs] JBossAS SVN: r105399 - in projects/ejb-book/trunk/chxx-employeeregistry/src: test/java/org/jboss/ejb3/examples/employeeregistry and 1 other directory.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun May 30 13:57:34 EDT 2010


Author: ALRubinger
Date: 2010-05-30 13:57:34 -0400 (Sun, 30 May 2010)
New Revision: 105399

Modified:
   projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/SimpleEmployee.java
   projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
Log:
[EJBBOOK-27] Start putting in JPA QL and Criteria API Query examples

Modified: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/SimpleEmployee.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/SimpleEmployee.java	2010-05-29 15:37:45 UTC (rev 105398)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/SimpleEmployee.java	2010-05-30 17:57:34 UTC (rev 105399)
@@ -120,4 +120,41 @@
    {
       return SimpleEmployee.class.getSimpleName() + " [id=" + id + ", name=" + name + "]";
    }
+
+   /**
+    * {@inheritDoc}
+    * @see java.lang.Object#hashCode()
+    */
+   @Override
+   public int hashCode()
+   {
+      final int prime = 31;
+      int result = 1;
+      result = prime * result + ((id == null) ? 0 : id.hashCode());
+      return result;
+   }
+
+   /**
+    * {@inheritDoc}
+    * @see java.lang.Object#equals(java.lang.Object)
+    */
+   @Override
+   public boolean equals(Object obj)
+   {
+      if (this == obj)
+         return true;
+      if (obj == null)
+         return false;
+      if (getClass() != obj.getClass())
+         return false;
+      SimpleEmployee other = (SimpleEmployee) obj;
+      if (id == null)
+      {
+         if (other.id != null)
+            return false;
+      }
+      else if (!id.equals(other.id))
+         return false;
+      return true;
+   }
 }

Modified: projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java	2010-05-29 15:37:45 UTC (rev 105398)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java	2010-05-30 17:57:34 UTC (rev 105399)
@@ -33,6 +33,9 @@
 import javax.persistence.EntityManager;
 import javax.persistence.GeneratedValue;
 import javax.persistence.IdClass;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
 
 import org.jboss.arquillian.api.Deployment;
 import org.jboss.arquillian.api.Run;
@@ -1357,6 +1360,82 @@
       Assert.assertTrue(postconditionMessage, EventTracker.preUpdate);
    }
 
+   /**
+    * Ensures we may look up an entity by a JPA QL Query
+    * @throws Exception
+    */
+   @Test
+   public void jpaQlFind() throws Exception
+   {
+      // Create an employee
+      final SimpleEmployee employee = new SimpleEmployee(ID_DAVE, NAME_DAVE);
+
+      // Persist, then lookup
+      txWrapper.wrapInTx(new Callable<Void>()
+      {
+
+         @Override
+         public Void call() throws Exception
+         {
+            // Get EM
+            final EntityManager em = emHook.getEntityManager();
+
+            // Persist
+            em.persist(employee);
+
+            // Lookup
+            final String jpaQlQuery = "FROM " + SimpleEmployee.class.getSimpleName() + " e WHERE e.name=?1";
+            final SimpleEmployee roundtrip = (SimpleEmployee) em.createQuery(jpaQlQuery).setParameter(1, NAME_DAVE)
+                  .getSingleResult();
+
+            // Test obtained as expected
+            Assert.assertEquals("Employee from JPA QL Query should equal the record added", employee, roundtrip);
+
+            // Return
+            return null;
+         }
+      });
+   }
+
+   /**
+    * Ensures we may look up an entity by a Criteria API Query
+    * @throws Exception
+    */
+   @Test
+   public void criertiaAPIFind() throws Exception
+   {
+      // Create an employee
+      final SimpleEmployee employee = new SimpleEmployee(ID_DAVE, NAME_DAVE);
+
+      // Persist, then lookup
+      txWrapper.wrapInTx(new Callable<Void>()
+      {
+
+         @Override
+         public Void call() throws Exception
+         {
+            // Get EM
+            final EntityManager em = emHook.getEntityManager();
+
+            // Persist
+            em.persist(employee);
+
+            // Lookup
+            final CriteriaBuilder builder = em.getCriteriaBuilder();
+            final CriteriaQuery<SimpleEmployee> query = builder.createQuery(SimpleEmployee.class);
+            Root<SimpleEmployee> root = query.from(SimpleEmployee.class);
+            query.select(root).where(builder.equal(root.get("name"), NAME_DAVE));
+            final SimpleEmployee roundtrip = (SimpleEmployee) em.createQuery(query).getSingleResult();
+
+            // Test obtained as expected
+            Assert.assertEquals("Employee from Criteria API Query should equal the record added", employee, roundtrip);
+
+            // Return
+            return null;
+         }
+      });
+   }
+
    //-------------------------------------------------------------------------------------||
    // Internal Helper Methods -----------------------------------------------------------||
    //-------------------------------------------------------------------------------------||




More information about the jboss-cvs-commits mailing list