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-1157 Allow the findBy method to use query hints defined on a repository.
Date Wed, 01 Jun 2016 23:06:51 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 0bb287f56 -> df1a0c9d4


DELTASPIKE-1157 Allow the findBy method to use query hints defined on a repository.


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

Branch: refs/heads/master
Commit: 9d66769d3b1c68e90e8644aa3470d9968633722a
Parents: 0bb287f
Author: John D. Ament <johndament@apache.org>
Authored: Wed Jun 1 18:37:28 2016 -0400
Committer: John D. Ament <johndament@apache.org>
Committed: Wed Jun 1 18:37:28 2016 -0400

----------------------------------------------------------------------
 .../org/apache/deltaspike/data/api/Query.java   |  2 ++
 .../impl/handler/EntityRepositoryHandler.java   | 19 ++++++++++-
 .../handler/EntityRepositoryHandlerTest.java    | 27 +++++++++++----
 .../test/service/IntermediateRepository.java    | 36 ++++++++++++++++++++
 .../service/SimpleIntermediateRepository.java   | 28 +++++++++++++++
 .../resources-jbossas7/test-persistence.xml     |  1 +
 6 files changed, 106 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9d66769d/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 e03410c..9e09150 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
@@ -19,6 +19,7 @@
 package org.apache.deltaspike.data.api;
 
 import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
@@ -37,6 +38,7 @@ import javax.persistence.QueryHint;
  */
 @Retention(RetentionPolicy.RUNTIME)
 @Target(ElementType.METHOD)
+@Inherited
 public @interface Query
 {
 

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9d66769d/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 ce5a473..46e496a 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
@@ -19,6 +19,7 @@
 package org.apache.deltaspike.data.impl.handler;
 
 import org.apache.deltaspike.data.api.EntityRepository;
+import org.apache.deltaspike.data.api.Query;
 import org.apache.deltaspike.data.impl.builder.QueryBuilder;
 import org.apache.deltaspike.data.impl.meta.RequiresTransaction;
 import org.apache.deltaspike.data.impl.property.Property;
@@ -31,14 +32,17 @@ import org.apache.deltaspike.data.spi.DelegateQueryHandler;
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
 import javax.persistence.PersistenceUnitUtil;
+import javax.persistence.QueryHint;
 import javax.persistence.TypedQuery;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.metamodel.SingularAttribute;
 
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -100,7 +104,20 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
     @Override
     public E findBy(PK primaryKey)
     {
-        return entityManager().find(entityClass(), primaryKey);
+        Query query = context.getMethod().getAnnotation(Query.class);
+        if (query != null && query.hints().length > 0)
+        {
+            Map<String, Object> hints = new HashMap<String, Object>();
+            for (QueryHint hint : query.hints())
+            {
+                hints.put(hint.name(), hint.value());
+            }
+            return entityManager().find(entityClass(), primaryKey, hints);
+        }
+        else
+        {
+            return entityManager().find(entityClass(), primaryKey);
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9d66769d/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 8ebc1fb..c72c311 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
@@ -27,6 +27,8 @@ import org.apache.deltaspike.data.test.service.ExtendedRepositoryAbstract;
 import org.apache.deltaspike.data.test.service.ExtendedRepositoryAbstract2;
 import org.apache.deltaspike.data.test.service.ExtendedRepositoryAbstract4;
 import org.apache.deltaspike.data.test.service.ExtendedRepositoryInterface;
+import org.apache.deltaspike.data.test.service.IntermediateRepository;
+import org.apache.deltaspike.data.test.service.SimpleIntermediateRepository;
 import org.apache.deltaspike.data.test.service.SimpleStringIdRepository;
 import org.apache.deltaspike.test.category.WebProfileCategory;
 import org.jboss.arquillian.container.test.api.Deployment;
@@ -34,12 +36,8 @@ 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;
@@ -59,8 +57,8 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
                 .addClasses(ExtendedRepositoryInterface.class)
                 .addClasses(ExtendedRepositoryAbstract.class)
                 .addClasses(ExtendedRepositoryAbstract2.class)
-                .addClasses(ExtendedRepositoryAbstract4.class)
-                .addClasses(SimpleStringIdRepository.class)
+                .addClasses(ExtendedRepositoryAbstract4.class, IntermediateRepository.class)
+                .addClasses(SimpleStringIdRepository.class, SimpleIntermediateRepository.class)
                 .addPackage(Simple.class.getPackage());
     }
 
@@ -79,6 +77,9 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
     @Inject
     private SimpleStringIdRepository stringIdRepo;
 
+    @Inject
+    private SimpleIntermediateRepository intermediate;
+
     @Test
     public void should_save() throws Exception
     {
@@ -463,4 +464,18 @@ public class EntityRepositoryHandlerTest extends TransactionalTestCase
         assertEquals(id, primaryKey);
     }
 
+    @Test
+    public void should_query_with_hints()
+    {
+        Simple simple = testData.createSimple("should_return_entity_primary_key");
+        Long id = simple.getId();
+
+        getEntityManager().flush();
+        getEntityManager().clear();
+
+        Simple found = intermediate.findBy(id);
+
+        assertEquals(id, found.getId());
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9d66769d/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/IntermediateRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/IntermediateRepository.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/IntermediateRepository.java
new file mode 100644
index 0000000..fb12773
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/IntermediateRepository.java
@@ -0,0 +1,36 @@
+/*
+ * 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.test.service;
+
+import org.apache.deltaspike.data.api.EntityRepository;
+import org.apache.deltaspike.data.api.Query;
+
+import javax.persistence.QueryHint;
+import java.io.Serializable;
+
+public interface IntermediateRepository<E, PK extends Serializable> extends EntityRepository<E,
PK>
+{
+    @Query(hints = {
+            @QueryHint(name = "openjpa.hint.OptimizeResultCount", value = "some.invalid.value"),
+            @QueryHint(name = "org.hibernate.comment", value = "I'm a little comment short
and stout")
+    })
+    @Override
+    E findBy(PK pk);
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9d66769d/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
new file mode 100644
index 0000000..38410a1
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleIntermediateRepository.java
@@ -0,0 +1,28 @@
+/*
+ * 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.test.service;
+
+import org.apache.deltaspike.data.api.Repository;
+import org.apache.deltaspike.data.test.domain.Simple;
+
+@Repository
+public interface SimpleIntermediateRepository extends IntermediateRepository<Simple, Long>
+{
+}

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/9d66769d/deltaspike/modules/data/impl/src/test/resources-jbossas7/test-persistence.xml
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/resources-jbossas7/test-persistence.xml
b/deltaspike/modules/data/impl/src/test/resources-jbossas7/test-persistence.xml
index bff9a02..f9b816a 100644
--- a/deltaspike/modules/data/impl/src/test/resources-jbossas7/test-persistence.xml
+++ b/deltaspike/modules/data/impl/src/test/resources-jbossas7/test-persistence.xml
@@ -22,6 +22,7 @@
         <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
         <properties>
             <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
+            <property name="hibernate.show_sql" value="true"/>
         </properties>
     </persistence-unit>
 </persistence>


Mime
View raw message