You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by da...@apache.org on 2015/06/28 22:12:36 UTC

deltaspike git commit: DELTASPIKE-673 CdiQueryInvocationContext#isNew does not work well with OpenJpa and detached entities

Repository: deltaspike
Updated Branches:
  refs/heads/master 2991e38f8 -> f22b3d491


DELTASPIKE-673 CdiQueryInvocationContext#isNew does not work well with OpenJpa and detached entities


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/f22b3d49
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/f22b3d49
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/f22b3d49

Branch: refs/heads/master
Commit: f22b3d49142a221fb8124078e2566d147345db78
Parents: 2991e38
Author: Daniel Cunha (soro) <da...@apache.org>
Authored: Sun Jun 28 17:01:48 2015 -0300
Committer: Daniel Cunha (soro) <da...@apache.org>
Committed: Sun Jun 28 17:01:48 2015 -0300

----------------------------------------------------------------------
 .../impl/handler/CdiQueryInvocationContext.java |  3 +-
 .../jpa/OpenJpaPersistenceUnitUtilDelegate.java | 47 +++++++++++++-------
 .../handler/EntityRepositoryHandlerTest.java    | 18 ++++++++
 3 files changed, 50 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f22b3d49/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
index da0bf05..45c3449 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
@@ -23,6 +23,7 @@ import org.apache.deltaspike.data.api.mapping.QueryInOutMapper;
 import org.apache.deltaspike.data.impl.meta.RepositoryMethod;
 import org.apache.deltaspike.data.impl.param.Parameters;
 import org.apache.deltaspike.data.impl.util.bean.Destroyable;
+import org.apache.deltaspike.data.impl.util.jpa.PersistenceUnitUtilDelegateFactory;
 import org.apache.deltaspike.data.spi.QueryInvocationContext;
 
 import javax.persistence.EntityManager;
@@ -91,7 +92,7 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
     {
         try
         {
-            return entityManager.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(entity) == null;
+            return PersistenceUnitUtilDelegateFactory.get(entityManager).getIdentifier(entity) == null;
         }
         catch (IllegalArgumentException e)
         {

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f22b3d49/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/OpenJpaPersistenceUnitUtilDelegate.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/OpenJpaPersistenceUnitUtilDelegate.java b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/OpenJpaPersistenceUnitUtilDelegate.java
index ba79788..8dd1e1d 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/OpenJpaPersistenceUnitUtilDelegate.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/OpenJpaPersistenceUnitUtilDelegate.java
@@ -18,6 +18,8 @@
  */
 package org.apache.deltaspike.data.impl.util.jpa;
 
+import org.apache.deltaspike.data.impl.util.EntityUtils;
+
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceUnitUtil;
 import java.lang.reflect.InvocationTargetException;
@@ -26,10 +28,12 @@ import java.lang.reflect.Method;
 public class OpenJpaPersistenceUnitUtilDelegate implements PersistenceUnitUtil
 {
     private final PersistenceUnitUtil persistenceUnitUtil;
+    private final EntityManager entityManager;
 
     public OpenJpaPersistenceUnitUtilDelegate(EntityManager entityManager)
     {
         this.persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
+        this.entityManager = entityManager;
     }
 
     @Override
@@ -47,28 +51,37 @@ public class OpenJpaPersistenceUnitUtilDelegate implements PersistenceUnitUtil
     public Object getIdentifier(Object entity)
     {
         final String methodName = "getIdObject";
-        final Object identifier = persistenceUnitUtil.getIdentifier(entity);
-        if (identifier != null)
+        try
         {
-            final Method method;
-            try
-            {
-                method = identifier.getClass().getMethod(methodName, null);
-                return method.invoke(identifier, null);
-            }
-            catch (NoSuchMethodException e)
+            if (!entityManager.contains(entity))
             {
-                // do nothing
+                entity = entityManager.getReference(entity.getClass(), EntityUtils.primaryKeyValue(entity));
             }
-            catch (InvocationTargetException e)
-            {
-                // do nothing
-            }
-            catch (IllegalAccessException e)
+
+            final Object identifier = persistenceUnitUtil.getIdentifier(entity);
+            if (identifier != null)
             {
-                // do nothing
-            }
+                final Method method;
 
+                method = identifier.getClass().getMethod(methodName, null);
+                return method.invoke(identifier, null);
+            }
+        }
+        catch (NoSuchMethodException e)
+        {
+            throw new RuntimeException(e);
+        }
+        catch (InvocationTargetException e)
+        {
+            throw new RuntimeException(e);
+        }
+        catch (IllegalAccessException e)
+        {
+            throw new RuntimeException(e);
+        }
+        catch (IllegalStateException e)
+        {
+            return null;
         }
         return null;
     }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/f22b3d49/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java
index 86104ed..df46d29 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandlerTest.java
@@ -407,12 +407,14 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
     {
         //given
         Simple simple = testData.createSimple("should_return_entity_primary_key");
+        Long id = simple.getId();
 
         //when
         Long primaryKey = repo.getPrimaryKey(simple);
 
         // then
         assertNotNull(primaryKey);
+        assertEquals(id, primaryKey);
     }
 
     @Test
@@ -428,6 +430,22 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
         assertNull(primaryKey);
     }
 
+    @Test
+    public void should_return_entity_primary_key_detached_entity()
+    {
+        //given
+        Simple simple = testData.createSimple("should_return_entity_primary_key");
+        Long id = simple.getId();
+
+        //when
+        entityManager.detach(simple);
+        Long primaryKey = repo.getPrimaryKey(simple);
+
+        // then
+        assertNotNull(primaryKey);
+        assertEquals(id, primaryKey);
+    }
+
     @Override
     protected EntityManager getEntityManager()
     {