deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johndam...@apache.org
Subject [1/2] deltaspike git commit: DELTASPIKE-1160 use return type of query method in native queries to build correct entity type
Date Tue, 07 Jun 2016 01:06:59 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 1054cd83c -> a1b2f717a


DELTASPIKE-1160 use return type of query method in native queries to build correct entity
type


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

Branch: refs/heads/master
Commit: 164e2eb49307b4f3dffe1796ce331900cc2c5906
Parents: 1054cd8
Author: mmain\heda151 <DanielAlexander.Heinrich.External@cassidian.com>
Authored: Mon Jun 6 12:16:26 2016 +0200
Committer: John D. Ament <johndament@apache.org>
Committed: Mon Jun 6 20:58:54 2016 -0400

----------------------------------------------------------------------
 .../org/apache/deltaspike/data/api/Query.java   |  7 ------
 .../impl/builder/AnnotatedQueryBuilder.java     | 23 ++++++++++++++++++--
 .../handler/EntityRepositoryHandlerTest.java    | 22 +++++++++++++++++++
 .../service/SimpleIntermediateRepository.java   |  2 +-
 .../test/service/SimpleStringIdRepository.java  |  9 ++++++++
 5 files changed, 53 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/164e2eb4/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/Query.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/Query.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/Query.java
index cd532c2..c5eef18 100755
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/Query.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/Query.java
@@ -79,11 +79,4 @@ public @interface Query
      * Exceptions thrown on non-single result queries.
      */
     SingleResultType singleResult() default SingleResultType.JPA;
-
-    /**
-     * For native queries only, whether or not this query returns the defined entity class
or not.
-     * Due to type erasure from generics, we don't have runtime information about the return
collection
-     */
-    boolean returnsEntity() default true;
-
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/164e2eb4/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
index 9e7cba2..7c94043 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
@@ -25,8 +25,11 @@ import org.apache.deltaspike.data.impl.meta.QueryInvocation;
 import org.apache.deltaspike.data.impl.param.Parameters;
 import org.apache.deltaspike.data.impl.util.jpa.QueryStringExtractorFactory;
 
+import javax.persistence.Entity;
 import javax.persistence.EntityManager;
 import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.util.List;
 import javax.enterprise.context.ApplicationScoped;
 
 import static org.apache.deltaspike.core.util.StringUtils.isNotEmpty;
@@ -72,9 +75,10 @@ public class AnnotatedQueryBuilder extends QueryBuilder
         else if (query.isNative())
         {
             String jpqlQuery = context.applyQueryStringPostProcessors(query.value());
-            if (query.returnsEntity())
+            Class<?> resultType = getQueryResultType(context.getMethod());
+            if (isEntityType(resultType))
             {
-                result = params.applyTo(entityManager.createNativeQuery(jpqlQuery, context.getEntityClass()));
+                result = params.applyTo(entityManager.createNativeQuery(jpqlQuery, resultType));
             }
             else
             {
@@ -90,4 +94,19 @@ public class AnnotatedQueryBuilder extends QueryBuilder
         return context.applyRestrictions(result);
     }
 
+    private boolean isEntityType(Class<?> cls)
+    {
+        return cls.getAnnotation(Entity.class) != null;
+    }
+
+    private Class<?> getQueryResultType(Method m)
+    {
+        Class<?> rt = m.getReturnType();
+        if (rt.isAssignableFrom(List.class) && rt != Object.class)
+        {
+            ParameterizedType pt = (ParameterizedType) m.getGenericReturnType();
+            return (Class<?>) pt.getActualTypeArguments()[0];
+        }
+        return rt;
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/164e2eb4/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 778552d..b9e60be 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
@@ -488,4 +488,26 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
         assertEquals(name, names.get(0));
     }
 
+    @Test
+    public void should_query_by_name()
+    {
+        String name = "should_return_entity_primary_key";
+        Simple simple = testData.createSimple(name);
+
+        Simple byName = stringIdRepo.findByName(name);
+
+        assertEquals(simple, byName);
+    }
+
+    @Test
+    public void should_query_list_by_name()
+    {
+        String name = "should_return_entity_primary_key";
+        Simple simple = testData.createSimple(name);
+
+        List<Simple> byName = stringIdRepo.findByName2(name);
+
+        assertEquals(byName.size(), 1);
+        assertEquals(simple, byName.get(0));
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/164e2eb4/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleIntermediateRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleIntermediateRepository.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleIntermediateRepository.java
index 53fa47d..cd258b7 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleIntermediateRepository.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleIntermediateRepository.java
@@ -36,6 +36,6 @@ public interface SimpleIntermediateRepository extends EntityRepository<Simple,
L
     })
     Simple findBy(Long id);
 
-    @Query(value = "select name from simple_table", isNative = true, returnsEntity = false)
+    @Query(value = "select name from simple_table", isNative = true)
     List<String> findAllNames();
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/164e2eb4/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleStringIdRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleStringIdRepository.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleStringIdRepository.java
index 63de9cd..2629a95 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleStringIdRepository.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleStringIdRepository.java
@@ -18,11 +18,20 @@
  */
 package org.apache.deltaspike.data.test.service;
 
+import java.util.List;
+
 import org.apache.deltaspike.data.api.EntityRepository;
+import org.apache.deltaspike.data.api.Query;
 import org.apache.deltaspike.data.api.Repository;
+import org.apache.deltaspike.data.test.domain.Simple;
 import org.apache.deltaspike.data.test.domain.SimpleStringId;
 
 @Repository
 public interface SimpleStringIdRepository extends EntityRepository<SimpleStringId, String>
 {
+    @Query("SELECT s FROM Simple s WHERE s.name = ?1")
+    Simple findByName(String name);
+
+    @Query("SELECT s FROM Simple s WHERE s.name = ?1")
+    List<Simple> findByName2(String name);
 }


Mime
View raw message