deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From daniels...@apache.org
Subject deltaspike git commit: DELTASPIKE-910 Add EntityRepository.getPrimaryKey(E entity)
Date Sun, 28 Jun 2015 18:54:48 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 85b49c11f -> 2991e38f8


DELTASPIKE-910 Add EntityRepository.getPrimaryKey(E entity)


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

Branch: refs/heads/master
Commit: 2991e38f817a593e540e63c798f91742ae33d057
Parents: 85b49c1
Author: Daniel Cunha (soro) <danielsoro@apache.org>
Authored: Sat Jun 27 04:42:06 2015 -0300
Committer: Daniel Cunha (soro) <danielsoro@apache.org>
Committed: Sun Jun 28 15:39:24 2015 -0300

----------------------------------------------------------------------
 .../deltaspike/data/api/EntityRepository.java   |  7 ++
 .../impl/handler/EntityRepositoryHandler.java   | 13 ++++
 .../jpa/OpenJpaPersistenceUnitUtilDelegate.java | 75 ++++++++++++++++++++
 .../jpa/PersistenceUnitUtilDelegateFactory.java | 41 +++++++++++
 .../handler/EntityRepositoryHandlerTest.java    | 53 ++++++++++----
 5 files changed, 175 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2991e38f/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java
index 730eb78..647a96e 100755
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/EntityRepository.java
@@ -175,4 +175,11 @@ public interface EntityRepository<E, PK extends Serializable> extends
Deactivata
      * @return                  Counter.
      */
     Long countLike(E example, SingularAttribute<E, ?>... attributes);
+
+    /**
+     * Return the id of the entity. Returns null if the entity does not yet have an id.
+     * @param example           Sample entity.
+     * @return                  id of the entity
+     */
+    PK getPrimaryKey(E example);
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2991e38f/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
index 2082e95..fabddec 100755
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
@@ -27,10 +27,12 @@ import org.apache.deltaspike.data.impl.property.Property;
 import org.apache.deltaspike.data.impl.property.query.NamedPropertyCriteria;
 import org.apache.deltaspike.data.impl.property.query.PropertyQueries;
 import org.apache.deltaspike.data.impl.util.EntityUtils;
+import org.apache.deltaspike.data.impl.util.jpa.PersistenceUnitUtilDelegateFactory;
 import org.apache.deltaspike.data.spi.DelegateQueryHandler;
 
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
+import javax.persistence.PersistenceUnitUtil;
 import javax.persistence.Table;
 import javax.persistence.TypedQuery;
 import javax.persistence.criteria.CriteriaQuery;
@@ -169,6 +171,12 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
     }
 
     @Override
+    public PK getPrimaryKey(E entity)
+    {
+        return (PK) persistenceUnitUtil().getIdentifier(entity);
+    }
+
+    @Override
     @RequiresTransaction
     public void remove(E entity)
     {
@@ -371,4 +379,9 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
         context.applyRestrictions(query);
         return query.getSingleResult();
     }
+
+    private PersistenceUnitUtil persistenceUnitUtil()
+    {
+        return PersistenceUnitUtilDelegateFactory.get(entityManager());
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2991e38f/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
new file mode 100644
index 0000000..ba79788
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/OpenJpaPersistenceUnitUtilDelegate.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.util.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceUnitUtil;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class OpenJpaPersistenceUnitUtilDelegate implements PersistenceUnitUtil
+{
+    private final PersistenceUnitUtil persistenceUnitUtil;
+
+    public OpenJpaPersistenceUnitUtilDelegate(EntityManager entityManager)
+    {
+        this.persistenceUnitUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
+    }
+
+    @Override
+    public boolean isLoaded(Object entity, String attributeName)
+    {
+        return persistenceUnitUtil.isLoaded(entity, attributeName);
+    }
+
+    @Override
+    public boolean isLoaded(Object entity)
+    {
+        return persistenceUnitUtil.isLoaded(entity);
+    }
+
+    public Object getIdentifier(Object entity)
+    {
+        final String methodName = "getIdObject";
+        final Object identifier = persistenceUnitUtil.getIdentifier(entity);
+        if (identifier != null)
+        {
+            final Method method;
+            try
+            {
+                method = identifier.getClass().getMethod(methodName, null);
+                return method.invoke(identifier, null);
+            }
+            catch (NoSuchMethodException e)
+            {
+                // do nothing
+            }
+            catch (InvocationTargetException e)
+            {
+                // do nothing
+            }
+            catch (IllegalAccessException e)
+            {
+                // do nothing
+            }
+
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2991e38f/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/PersistenceUnitUtilDelegateFactory.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/PersistenceUnitUtilDelegateFactory.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/PersistenceUnitUtilDelegateFactory.java
new file mode 100644
index 0000000..ac2577d
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/util/jpa/PersistenceUnitUtilDelegateFactory.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.util.jpa;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnitUtil;
+
+public class PersistenceUnitUtilDelegateFactory
+{
+    private PersistenceUnitUtilDelegateFactory()
+    {
+    }
+
+    public static PersistenceUnitUtil get(EntityManager entityManager)
+    {
+        final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
+        final String vendorName = (String) entityManagerFactory.getProperties().get("VendorName");
+        if (vendorName != null && "openjpa".equalsIgnoreCase(vendorName))
+        {
+            return new OpenJpaPersistenceUnitUtilDelegate(entityManager);
+        }
+        return entityManagerFactory.getPersistenceUnitUtil();
+    }
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/2991e38f/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 c885a4d..86104ed 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
@@ -18,20 +18,6 @@
  */
 package org.apache.deltaspike.data.impl.handler;
 
-import static org.apache.deltaspike.data.test.util.TestDeployments.initDeployment;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-import java.util.List;
-
-import javax.enterprise.inject.Produces;
-import javax.inject.Inject;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import javax.persistence.metamodel.SingularAttribute;
-
 import org.apache.deltaspike.data.test.TransactionalTestCase;
 import org.apache.deltaspike.data.test.domain.Simple;
 import org.apache.deltaspike.data.test.domain.Simple2;
@@ -46,6 +32,19 @@ import org.jboss.shrinkwrap.api.Archive;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
 
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.metamodel.SingularAttribute;
+import java.util.List;
+
+import static org.apache.deltaspike.data.test.util.TestDeployments.initDeployment;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
 @Category(WebProfileCategory.class)
 public class EntityRepositoryHandlerTest extends TransactionalTestCase
 {
@@ -403,6 +402,32 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
         assertEquals("EntitySimple4", entityName2);
     }
 
+    @Test
+    public void should_return_entity_primary_key()
+    {
+        //given
+        Simple simple = testData.createSimple("should_return_entity_primary_key");
+
+        //when
+        Long primaryKey = repo.getPrimaryKey(simple);
+
+        // then
+        assertNotNull(primaryKey);
+    }
+
+    @Test
+    public void should_return_null_primary_key()
+    {
+        //given
+        Simple simple = new Simple("should_return_null_primary_key");
+
+        //when
+        Long primaryKey = repo.getPrimaryKey(simple);
+
+        // then
+        assertNull(primaryKey);
+    }
+
     @Override
     protected EntityManager getEntityManager()
     {


Mime
View raw message