openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dwo...@apache.org
Subject svn commit: r800568 - in /openjpa/trunk: openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/ openjpa-persistence/src/main/java/org/apache/op...
Date Mon, 03 Aug 2009 21:14:44 GMT
Author: dwoods
Date: Mon Aug  3 21:14:43 2009
New Revision: 800568

URL: http://svn.apache.org/viewvc?rev=800568&view=rev
Log:
OPENJPA-1107 Updated TraversableResolver that calls only our OpenJPAPersistenceUtil.  Updated
OpenJPAPersistenceUtil.isLoaded() to handle UnsupportedOperationException when checking a
detached entity and returns LoadState.UNKNOWN.  Also added a testcase in TestPersistenceUtil
to verify that caller gets isLoaded()==true for a detached entity and fixed a trace output
in TestConstraints.

Modified:
    openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/TraversableResolverImpl.java

Modified: openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java?rev=800568&r1=800567&r2=800568&view=diff
==============================================================================
--- openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
(original)
+++ openjpa/trunk/openjpa-integration/validation/src/test/java/org/apache/openjpa/integration/validation/TestConstraints.java
Mon Aug  3 21:14:43 2009
@@ -1188,6 +1188,7 @@
             p.setFirstName("Java");
             p.setLastName("Joe");
             // use invalid Address, which should cause CVEs due to @Valid
+            //a = em.getReference(Address.class, a.getId());
             assertNotNull(a);
             p.setHomeAddress(a);
             // persist, which should cause a CVE
@@ -1201,7 +1202,7 @@
             Set<ConstraintViolation<?>> cves = e.getConstraintViolations();
             assertNotNull(cves);
             for (ConstraintViolation<?> cv: cves) {
-                getLog().trace("CVE Contains ConstraintViolation = " + cv);
+                getLog().trace("CVE Contains ConstraintViolation = " + cv.getMessage());
             }
             assertEquals("Wrong number of embedded ConstraintViolation failures",
                 5, cves.size());

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java?rev=800568&r1=800567&r2=800568&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/util/TestPersistenceUtil.java
Mon Aug  3 21:14:43 2009
@@ -73,7 +73,56 @@
         verifyIsLoadedEagerState(false);       
     }
 
-    
+    /*
+     * Verifies that an entity and attributes are considered loaded if they
+     * are assigned by the application.
+     */
+    public void testIsApplicationLoaded() {
+        PersistenceUtil putil = Persistence.getPersistenceUtil();
+        EntityManager em = emf.createEntityManager();
+        EagerEntity ee = createEagerEntity();
+        
+        em.getTransaction().begin();
+        em.persist(ee);
+        em.getTransaction().commit();
+        em.clear();
+        
+        ee = em.getReference(EagerEntity.class, ee.getId());
+        assertNotNull(ee);
+        assertEagerLoadState(putil, ee, false);
+        
+        ee.setName("AppEagerName");
+        EagerEmbed emb = createEagerEmbed();
+        ee.setEagerEmbed(emb);
+        // Assert fields are loaded via application loading
+        assertEagerLoadState(putil, ee, true);
+        // Vfy the set values are applied to the entity
+        assertEquals("AppEagerName", ee.getName());
+        assertEquals(emb, ee.getEagerEmbed());
+        
+        em.close();
+    }
+        
+    /*
+     * Verifies that an entity and attributes are considered loaded if they
+     * are in the detached state.
+     */
+    public void testIsDetachLoaded() {
+        PersistenceUtil putil = Persistence.getPersistenceUtil();
+        EntityManager em = emf.createEntityManager();
+        EagerEntity ee = createEagerEntity();
+        
+        em.getTransaction().begin();
+        em.persist(ee);
+        em.getTransaction().commit();
+        em.clear();
+        
+        // should be true, as detached is treated as LoadState.UNKNOWN
+        assertEquals(true, putil.isLoaded(ee));
+        
+        em.close();
+    }
+
     private void verifyIsLoadedEagerState(boolean loaded) {
         PersistenceUtil putil = Persistence.getPersistenceUtil();
         EntityManager em = emf.createEntityManager();
@@ -136,36 +185,6 @@
         em.close();
     }
 
-    /*
-     * Verifies that an entity and attributes are considered loaded if they
-     * are assigned by the application.
-     */
-    public void testIsApplicationLoaded() {
-        PersistenceUtil putil = Persistence.getPersistenceUtil();
-        EntityManager em = emf.createEntityManager();
-        EagerEntity ee = createEagerEntity();
-        
-        em.getTransaction().begin();
-        em.persist(ee);
-        em.getTransaction().commit();
-        em.clear();
-        
-        ee = em.getReference(EagerEntity.class, ee.getId());
-        assertNotNull(ee);
-        assertEagerLoadState(putil, ee, false);
-        
-        ee.setName("AppEagerName");
-        EagerEmbed emb = createEagerEmbed();
-        ee.setEagerEmbed(emb);
-        // Assert fields are loaded via application loading
-        assertEagerLoadState(putil, ee, true);
-        // Vfy the set values are applied to the entity
-        assertEquals("AppEagerName", ee.getName());
-        assertEquals(emb, ee.getEagerEmbed());
-        
-        em.close();
-    }
-        
     private EagerEntity createEagerEntity() {
         EagerEntity ee = new EagerEntity();
         ee.setId(new Random().nextInt());

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java?rev=800568&r1=800567&r2=800568&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/OpenJPAPersistenceUtil.java
Mon Aug  3 21:14:43 2009
@@ -165,31 +165,37 @@
     private static LoadState isLoaded(OpenJPAStateManager sm, String attr, 
         HashSet<OpenJPAStateManager> pcs) {
         boolean isLoaded = true;
-        BitSet loadSet = sm.getLoaded();
-        if (attr != null) {
-            FieldMetaData fmd = sm.getMetaData().getField(attr);
-            // Could not find field metadata for the specified attribute.
-            if (fmd == null) {
-                return LoadState.UNKNOWN;
-            }
-            // Otherwise, return the load state
-            if(!loadSet.get(fmd.getIndex())) {
-                return LoadState.NOT_LOADED;
+        try {
+            BitSet loadSet = sm.getLoaded();
+            if (attr != null) {
+                FieldMetaData fmd = sm.getMetaData().getField(attr);
+                // Could not find field metadata for the specified attribute.
+                if (fmd == null) {
+                    return LoadState.UNKNOWN;
+                }
+                // Otherwise, return the load state
+                if(!loadSet.get(fmd.getIndex())) {
+                    return LoadState.NOT_LOADED;
+                }
             }
-        }
-        FieldMetaData[] fmds = sm.getMetaData().getFields();
-        // Check load state of all persistent eager fetch attributes
-        if (fmds != null && fmds.length > 0) {
-            pcs = addToLoadSet(pcs, sm);
-            for (FieldMetaData fmd : fmds) {
-                if (fmd.isInDefaultFetchGroup()) {
-                    if (!isLoadedField(sm, fmd, pcs)) {
-                        isLoaded = false;
-                        break;
+            FieldMetaData[] fmds = sm.getMetaData().getFields();
+            // Check load state of all persistent eager fetch attributes
+            if (fmds != null && fmds.length > 0) {
+                pcs = addToLoadSet(pcs, sm);
+                for (FieldMetaData fmd : fmds) {
+                    if (fmd.isInDefaultFetchGroup()) {
+                        if (!isLoadedField(sm, fmd, pcs)) {
+                            isLoaded = false;
+                            break;
+                        }
                     }
                 }
+                pcs.remove(sm);
             }
-            pcs.remove(sm);
+        } catch (RuntimeException e) {
+            // treat any exceptions, like UnsupportedOperationException
+            // for detached entities, as LoadState.UNKNOWN
+            return LoadState.UNKNOWN;
         }
         return isLoaded ? LoadState.LOADED : LoadState.NOT_LOADED;        
     }

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/TraversableResolverImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/TraversableResolverImpl.java?rev=800568&r1=800567&r2=800568&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/TraversableResolverImpl.java
(original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/validation/TraversableResolverImpl.java
Mon Aug  3 21:14:43 2009
@@ -56,36 +56,17 @@
         Node traversableProperty, Class<?> rootBeanType,
         Path pathToTraversableObject, ElementType elementType) {
 
-        if (traversableObject == null) {
-            return true;
-        }
-        
-        // Identical behavior to spec defined default provider implementation
-        return javax.persistence.Persistence.getPersistenceUtil().isLoaded(
-            traversableObject, traversableProperty.getName());
-        
         /*
-         * Improved OpenJPA version of the default provider implementation
-         * 
-         * The below code causes -
-         * Caused by: javax.validation.ValidationException: java.lang.UnsupportedOperationException
-         * at org.apache.openjpa.persistence.validation.TraversableResolverImpl.isReachable
-         *      (TraversableResolverImpl.java:73)
-         * at org.hibernate.validation.engine.resolver.SingleThreadCachedTraversableResolver.isReachable
-         *      (SingleThreadCachedTraversableResolver.java:47)
-         *
-        try {
-            if (OpenJPAPersistenceUtil.isLoaded(emf, traversableObject, 
-                traversableProperty.getName()) == LoadState.NOT_LOADED) {
-                return false;
-            }
-        } catch (Exception e) {
-            // BV Spec Section 3.5.2 - any exceptions must be wrapped
-            throw new ValidationException(e);
+         * OpenJPA optimized version of the default provider implementation,
+         * which doesn't ask all the providers on the classpath about the obj.
+         */
+        if (OpenJPAPersistenceUtil.isLoaded(emf, traversableObject, 
+            traversableProperty.getName()) == LoadState.NOT_LOADED) {
+            return false;
+        } else {
+            // LoadState.LOADED or LoadState.UNKNOWN
+            return true;
         }
-        // LoadState.LOADED or LoadState.UNKNOWN
-        return true;
-        */
     }
 
     /* (non-Javadoc) isCascadable() is called by the Validator after 
@@ -100,7 +81,7 @@
         Node traversableProperty, Class<?> rootBeanType,
         Path pathToTraversableObject, ElementType elementType) {
 
-        // BV Spec Section 3.5.2 says to always return true for JPA
+        // BV Spec Section 3.5.2 says to always return true for JPA ???
         return true;
     }
 }



Mime
View raw message