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-701 Add method in EntityRepository to merge a detached entity and remove it
Date Mon, 08 Jun 2015 18:59:36 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master aa64f7144 -> 45639a700


DELTASPIKE-701 Add method in EntityRepository to merge a detached entity and remove it


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

Branch: refs/heads/master
Commit: 45639a700bda7a514ae6d48ff9168911f773a9dd
Parents: aa64f71
Author: Daniel Cunha (soro) <danielsoro@apache.org>
Authored: Mon Jun 8 15:57:59 2015 -0300
Committer: Daniel Cunha (soro) <danielsoro@apache.org>
Committed: Mon Jun 8 15:57:59 2015 -0300

----------------------------------------------------------------------
 .../deltaspike/data/api/EntityRepository.java   |  8 ++++++-
 .../impl/handler/EntityRepositoryHandler.java   | 22 ++++++++++++++------
 .../handler/EntityRepositoryHandlerTest.java    | 15 +++++++++++++
 3 files changed, 38 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/45639a70/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 f879c38..730eb78 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
@@ -74,6 +74,13 @@ public interface EntityRepository<E, PK extends Serializable> extends
Deactivata
     void removeAndFlush(E entity);
 
     /**
+     * Convenience access to {@link javax.persistence.EntityManager#remove(Object)}
+     * with an detached entity.
+     * @param entity            Entity to remove.
+     */
+    void attachAndRemove(E entity);
+
+    /**
      * Convenience access to {@link javax.persistence.EntityManager#refresh(Object)}.
      * @param entity            Entity to refresh.
      */
@@ -168,5 +175,4 @@ public interface EntityRepository<E, PK extends Serializable> extends
Deactivata
      * @return                  Counter.
      */
     Long countLike(E example, SingularAttribute<E, ?>... attributes);
-
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/45639a70/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 7d87e1d..64700c8 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
@@ -47,8 +47,8 @@ import org.apache.deltaspike.data.spi.QueryInvocationContext;
 /**
  * Implement basic functionality from the {@link EntityRepository}.
  *
- * @param <E>   Entity type.
- * @param <PK>  Primary key type, must be a serializable.
+ * @param <E>  Entity type.
+ * @param <PK> Primary key type, must be a serializable.
  */
 public class EntityRepositoryHandler<E, PK extends Serializable>
         implements EntityRepository<E, PK>, DelegateQueryHandler
@@ -182,6 +182,17 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
 
     @Override
     @RequiresTransaction
+    public void attachAndRemove(E entity)
+    {
+        if (!entityManager().contains(entity))
+        {
+            entity = entityManager().merge(entity);
+        }
+        remove(entity);
+    }
+
+    @Override
+    @RequiresTransaction
     public void flush()
     {
         entityManager().flush();
@@ -230,7 +241,7 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
     }
 
     private void addParameters(TypedQuery<?> query, E example, List<Property<Object>>
properties,
-            boolean useLikeOperator)
+                               boolean useLikeOperator)
     {
         for (Property<Object> property : properties)
         {
@@ -286,12 +297,12 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
     {
         List<String> names = extractPropertyNames(attributes);
         List<Property<Object>> properties = PropertyQueries.createQuery(entityClass())
-                .addCriteria(new NamedPropertyCriteria(names.toArray(new String[] {}))).getResultList();
+                .addCriteria(new NamedPropertyCriteria(names.toArray(new String[]{}))).getResultList();
         return properties;
     }
 
     private List<E> executeExampleQuery(E example, int start, int max, boolean useLikeOperator,
-            SingularAttribute<E, ?>... attributes)
+                                        SingularAttribute<E, ?>... attributes)
     {
         // Not sure if this should be the intended behaviour
         // when we don't get any attributes maybe we should
@@ -335,5 +346,4 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
         addParameters(query, example, properties, useLikeOperator);
         return query.getSingleResult();
     }
-
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/45639a70/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 831ba77..a86bc2b 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
@@ -352,6 +352,21 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
         assertNull(lookup);
     }
 
+    @Test
+    public void should_remove_detach_entity() {
+        //given
+        Simple simple = testData.createSimple("testeAttachAndRemove");
+
+        //when
+        repo.detach(simple);
+        repo.attachAndRemove(simple);
+        repo.flush();
+        Simple lookup = entityManager.find(Simple.class, simple.getId());
+
+        // then
+        assertNull(lookup);
+    }
+
     @Override
     protected EntityManager getEntityManager()
     {


Mime
View raw message