Permalink
Browse files

DATAJPA-8 - Introduced support for QueryDsl specifications

Extracted methods taking a Specification from JpaRepository into JpaSpecificationExecutor. Introduced QueryDslPredicateExecutor that takes QueryDsl Predicate objects to execute them. Introduced QueryDsl specific subclass to execute Predicate instances. Extended JpaRepositoryFactory to rather use the QueryDsl specific implementation if the actual user repository implements QueryDslPredicateExecutor.
  • Loading branch information...
1 parent 916f4fe commit 98b2a892b062de5bbeaf8a3cc5ba18223ced252b @olivergierke olivergierke committed Dec 29, 2010
View
46 pom.xml
@@ -15,6 +15,7 @@
<hibernate.version>3.6.0.Final</hibernate.version>
<openjpa.version>2.0.0</openjpa.version>
<aspectj.version>1.6.8</aspectj.version>
+ <querydsl.version>2.0.6</querydsl.version>
</properties>
<dependencies>
@@ -104,6 +105,20 @@
</exclusion>
</exclusions>
</dependency>
+
+ <!-- QueryDsl -->
+ <dependency>
+ <groupId>com.mysema.querydsl</groupId>
+ <artifactId>querydsl-apt</artifactId>
+ <version>${querydsl.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>com.mysema.querydsl</groupId>
+ <artifactId>querydsl-jpa</artifactId>
+ <version>${querydsl.version}</version>
+ </dependency>
</dependencies>
<build>
@@ -167,6 +182,24 @@
<target>1.5</target>
</configuration>
</plugin>
+
+ <plugin>
+ <groupId>com.mysema.maven</groupId>
+ <artifactId>maven-apt-plugin</artifactId>
+ <version>1.0</version>
+ <executions>
+ <execution>
+ <phase>process-test-sources</phase>
+ <goals>
+ <goal>test-process</goal>
+ </goals>
+ <configuration>
+ <outputDirectory>target/generated-test-sources/java</outputDirectory>
+ <processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
</plugins>
</build>
@@ -181,5 +214,18 @@
<name>EclipseLink Repo</name>
<url>http://www.eclipse.org/downloads/download.php?r=1&amp;nf=1&amp;file=/rt/eclipselink/maven.repo</url>
</repository>
+ <repository>
+ <id>querydsl</id>
+ <name>QueryDsl</name>
+ <url>http://source.mysema.com/maven2/releases</url>
+ </repository>
</repositories>
+
+ <pluginRepositories>
+ <pluginRepository>
+ <id>querydsl</id>
+ <name>QueryDsl</name>
+ <url>http://source.mysema.com/maven2/releases</url>
+ </pluginRepository>
+ </pluginRepositories>
</project>
@@ -16,11 +16,7 @@
package org.springframework.data.jpa.repository;
import java.io.Serializable;
-import java.util.List;
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.Pageable;
-import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.Repository;
@@ -34,35 +30,6 @@
PagingAndSortingRepository<T, ID> {
/**
- * Returns a single entity matching the given {@link Specification}.
- *
- * @param spec
- * @return
- */
- T findOne(Specification<T> spec);
-
-
- /**
- * Returns all entities matching the given {@link Specification}.
- *
- * @param spec
- * @return
- */
- List<T> findAll(Specification<T> spec);
-
-
- /**
- * Returns a {@link Page} of entities matching the given
- * {@link Specification}.
- *
- * @param spec
- * @param pageable
- * @return
- */
- Page<T> findAll(Specification<T> spec, Pageable pageable);
-
-
- /**
* Flushes all pending changes to the database.
*/
void flush();
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2008-2010 the original author or authors.
+ *
+ * Licensed 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.springframework.data.jpa.repository;
+
+import java.util.List;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
+
+
+/**
+ * Interface to allow execution of {@link Specification}s based on the JPA
+ * criteria API.
+ *
+ * @author Oliver Gierke
+ */
+public interface JpaSpecificationExecutor<T> {
+
+ /**
+ * Returns a single entity matching the given {@link Specification}.
+ *
+ * @param spec
+ * @return
+ */
+ T findOne(Specification<T> spec);
+
+
+ /**
+ * Returns all entities matching the given {@link Specification}.
+ *
+ * @param spec
+ * @return
+ */
+ List<T> findAll(Specification<T> spec);
+
+
+ /**
+ * Returns a {@link Page} of entities matching the given
+ * {@link Specification}.
+ *
+ * @param spec
+ * @param pageable
+ * @return
+ */
+ Page<T> findAll(Specification<T> spec, Pageable pageable);
+
+
+ /**
+ * Returns the number of instances that the given {@link Specification} will
+ * return.
+ *
+ * @param spec the {@link Specification} to count instances for
+ * @return the number of instances
+ */
+ Long count(Specification<T> spec);
+}
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2008-2010 the original author or authors.
+ *
+ * Licensed 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.springframework.data.jpa.repository;
+
+import java.util.List;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+import com.mysema.query.types.OrderSpecifier;
+import com.mysema.query.types.Predicate;
+
+
+/**
+ * Interface to allow execution of QueryDsl {@link Predicate} instances.
+ *
+ * @author Oliver Gierke
+ */
+public interface QueryDslPredicateExecutor<T> {
+
+ /**
+ * Returns a single entity matching the given {@link Predicate}.
+ *
+ * @param spec
+ * @return
+ */
+ T findOne(Predicate predicate);
+
+
+ /**
+ * Returns all entities matching the given {@link Predicate}.
+ *
+ * @param spec
+ * @return
+ */
+ List<T> findAll(Predicate predicate);
+
+
+ /**
+ * Returns all entities matching the given {@link Predicate} applying the
+ * given {@link OrderSpecifier}s.
+ *
+ * @param predicate
+ * @param orders
+ * @return
+ */
+ List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders);
+
+
+ /**
+ * Returns a {@link Page} of entities matching the given {@link Predicate}.
+ *
+ * @param spec
+ * @param pageable
+ * @return
+ */
+ Page<T> findAll(Predicate spec, Pageable pageable);
+
+
+ /**
+ * Returns the number of instances that the given {@link Predicate} will
+ * return.
+ *
+ * @param spec the {@link Predicate} to count instances for
+ * @return the number of instances
+ */
+ Long count(Predicate spec);
+}
@@ -16,9 +16,11 @@
package org.springframework.data.jpa.repository.support;
import java.io.Serializable;
+import java.util.Arrays;
import javax.persistence.EntityManager;
+import org.springframework.data.jpa.repository.QueryDslPredicateExecutor;
import org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy;
import org.springframework.data.jpa.repository.query.QueryExtractor;
import org.springframework.data.repository.query.QueryLookupStrategy;
@@ -83,7 +85,11 @@ public JpaRepositoryFactory(EntityManager entityManager) {
Class<T> domainClass, Class<?> repositoryInterface,
EntityManager entityManager) {
- return new SimpleJpaRepository<T, ID>(domainClass, entityManager);
+ if (isQueryDslExecutor(repositoryInterface)) {
+ return new QueryDslJpaRepository<T, ID>(domainClass, entityManager);
+ } else {
+ return new SimpleJpaRepository<T, ID>(domainClass, entityManager);
+ }
}
@@ -99,7 +105,25 @@ public JpaRepositoryFactory(EntityManager entityManager) {
protected Class<? extends RepositorySupport> getRepositoryClass(
Class<?> repositoryInterface) {
- return SimpleJpaRepository.class;
+ if (isQueryDslExecutor(repositoryInterface)) {
+ return QueryDslJpaRepository.class;
+ } else {
+ return SimpleJpaRepository.class;
+ }
+ }
+
+
+ /**
+ * Returns whether the given repository interface requires a QueryDsl
+ * specific implementation to be chosen.
+ *
+ * @param repositoryInterface
+ * @return
+ */
+ private boolean isQueryDslExecutor(Class<?> repositoryInterface) {
+
+ return Arrays.asList(repositoryInterface.getInterfaces()).contains(
+ QueryDslPredicateExecutor.class);
}
Oops, something went wrong.

0 comments on commit 98b2a89

Please sign in to comment.