You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by jr...@apache.org on 2008/09/30 21:42:11 UTC

svn commit: r700563 [3/3] - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/kernel/StateManagerImpl.java openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java?rev=700563&r1=700562&r2=700563&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java (original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/embed/TestEmbedded.java Tue Sep 30 12:42:11 2008
@@ -18,7 +18,11 @@
  */
 package org.apache.openjpa.persistence.embed;
 
+import java.util.List;
+
 import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
 import org.apache.openjpa.persistence.test.SingleEMFTestCase;
 
 public class TestEmbedded extends SingleEMFTestCase {
@@ -42,5 +46,71 @@
         em.persist(a);
         em.getTransaction().commit();
     }
+    
+    /*
+     * This variation verifies that an embedded entity can be accessed after
+     * being detached.  An entity /w embedded is persisted and then queried.
+     * The em is closed, detaching the entities, and then a getter is called
+     * on the embeddeded.  If the embedded is still attached (it should not be)
+     * an IllegalStateException will be thrown.    
+     * 
+     * JIRA Ref: OPENJPA-733
+     * Authors: Chris Tillman, Jeremy Bauer
+     */    
+    public void testDetachedQueryEmbedded() {
+        Address a = new Address();
+        a.setStreetAddress("456 Main St");
+        a.setCity("New York");
+        a.setState("NY");
+        a.setZip(12955);
+        Geocode g = new Geocode();
+        g.setLatitude(1.0f);
+        g.setLongtitude(2.0f);
+        a.setGeocode(g);
+        
+        persistAddress(a);
+        
+        Address a2 = queryAddresses(
+                "select address from Address address" +
+                " where address.streetAddress = '456 Main St'").get(0);
+        
+        assertEquals(a2.getGeocode().getLatitude(),1.0f);
+    }
 
-}
\ No newline at end of file
+    private void persistAddress(Address address) {
+        final EntityManager em = emf.createEntityManager();
+        final EntityTransaction tx = em.getTransaction();
+
+        tx.begin();
+
+        try {
+            em.persist(address);
+            tx.commit();
+        } finally {
+            if (tx.isActive()) {
+                tx.rollback();
+            }
+            em.close();
+        }
+        assertEquals(address.getGeocode().getLatitude(),1.0f);
+    }
+
+    private List<Address> queryAddresses(String query) {
+        final EntityManager em = emf.createEntityManager();
+        final EntityTransaction tx = em.getTransaction();
+
+        tx.begin();
+
+        try {
+            final List<Address> list = (List<Address>) em.createQuery(query)
+                            .getResultList();
+            tx.commit();
+            return list;
+        } finally {
+            if (tx.isActive()) {
+                tx.rollback();
+            }
+            em.close();
+        }
+    }
+}