diff --git a/pom.xml b/pom.xml
index cc5126fd7..c704025b7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,7 +1,6 @@
4.0.0
-
org.springframework.data
spring-data-jpa
1.0.0.BUILD-SNAPSHOT
@@ -58,6 +57,7 @@
2.0.0
2.2.0
1.6.8
+ 2.1.1
4.8.1
2.0.0
1.6.1
@@ -385,6 +385,20 @@
+
+
+
+ com.mysema.querydsl
+ querydsl-apt
+ ${querydsl.version}
+ provided
+
+
+
+ com.mysema.querydsl
+ querydsl-jpa
+ ${querydsl.version}
+
@@ -502,6 +516,24 @@
+
+
+ com.mysema.maven
+ maven-apt-plugin
+ 1.0
+
+
+ generate-test-sources
+
+ test-process
+
+
+ target/generated-sources/test-annotations
+ com.mysema.query.apt.jpa.JPAAnnotationProcessor
+
+
+
+
@@ -566,6 +598,11 @@
EclipseLink Repo
http://www.eclipse.org/downloads/download.php?r=1&nf=1&file=/rt/eclipselink/maven.repo
+
+ querydsl
+ QueryDsl
+ http://source.mysema.com/maven2/releases
+
@@ -575,5 +612,10 @@
Spring Framework Maven Release Repository
http://maven.springframework.org/release
+
+ querydsl
+ QueryDsl
+ http://source.mysema.com/maven2/releases
+
\ No newline at end of file
diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java
index fdb5fa702..fda8f6231 100644
--- a/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java
+++ b/src/main/java/org/springframework/data/jpa/repository/JpaRepository.java
@@ -148,35 +148,6 @@ public interface JpaRepository extends
Page findAll(Pageable pageable);
- /**
- * Returns a single entity matching the given {@link Specification}.
- *
- * @param spec
- * @return
- */
- T findOne(Specification spec);
-
-
- /**
- * Returns all entities matching the given {@link Specification}.
- *
- * @param spec
- * @return
- */
- List findAll(Specification spec);
-
-
- /**
- * Returns a {@link Page} of entities matching the given
- * {@link Specification}.
- *
- * @param spec
- * @param pageable
- * @return
- */
- Page findAll(Specification spec, Pageable pageable);
-
-
/**
* Flushes all pending changes to the database.
*/
diff --git a/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java b/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java
new file mode 100644
index 000000000..c62bbecfe
--- /dev/null
+++ b/src/main/java/org/springframework/data/jpa/repository/JpaSpecificationExecutor.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2008-2011 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 {
+
+ /**
+ * Returns a single entity matching the given {@link Specification}.
+ *
+ * @param spec
+ * @return
+ */
+ T findOne(Specification spec);
+
+
+ /**
+ * Returns all entities matching the given {@link Specification}.
+ *
+ * @param spec
+ * @return
+ */
+ List findAll(Specification spec);
+
+
+ /**
+ * Returns a {@link Page} of entities matching the given
+ * {@link Specification}.
+ *
+ * @param spec
+ * @param pageable
+ * @return
+ */
+ Page findAll(Specification 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 spec);
+}
diff --git a/src/main/java/org/springframework/data/jpa/repository/QueryDslPredicateExecutor.java b/src/main/java/org/springframework/data/jpa/repository/QueryDslPredicateExecutor.java
new file mode 100644
index 000000000..3c2b9394b
--- /dev/null
+++ b/src/main/java/org/springframework/data/jpa/repository/QueryDslPredicateExecutor.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2008-2011 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 {
+
+ /**
+ * 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 findAll(Predicate predicate);
+
+
+ /**
+ * Returns all entities matching the given {@link Predicate} applying the
+ * given {@link OrderSpecifier}s.
+ *
+ * @param predicate
+ * @param orders
+ * @return
+ */
+ List findAll(Predicate predicate, OrderSpecifier>... orders);
+
+
+ /**
+ * Returns a {@link Page} of entities matching the given {@link Predicate}.
+ *
+ * @param predicate
+ * @param pageable
+ * @return
+ */
+ Page findAll(Predicate predicate, Pageable pageable);
+
+
+ /**
+ * Returns the number of instances that the given {@link Predicate} will
+ * return.
+ *
+ * @param predicate the {@link Predicate} to count instances for
+ * @return the number of instances
+ */
+ Long count(Predicate predicate);
+}
diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java
index 6543dcb73..7b1ad8de4 100644
--- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java
+++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java
@@ -19,6 +19,7 @@ import java.io.Serializable;
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.jpa.repository.utils.JpaClassUtils;
@@ -27,6 +28,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
import org.springframework.data.repository.support.RepositoryFactorySupport;
import org.springframework.data.repository.support.RepositoryMetadata;
import org.springframework.util.Assert;
+import org.springframework.util.ClassUtils;
/**
@@ -36,6 +38,10 @@ import org.springframework.util.Assert;
*/
public class JpaRepositoryFactory extends RepositoryFactorySupport {
+ private static final boolean QUERY_DSL_PRESENT = ClassUtils.isPresent(
+ "com.mysema.query.types.Predicate",
+ JpaRepositoryFactory.class.getClassLoader());
+
private final EntityManager entityManager;
private final QueryExtractor extractor;
@@ -81,9 +87,15 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
protected Object getTargetRepository(
RepositoryMetadata metadata, EntityManager entityManager) {
- JpaEntityInformation entityMetadata =
- getEntityInformation((Class) metadata.getDomainClass());
- return new SimpleJpaRepository(entityMetadata, entityManager);
+ Class> repositoryInterface = metadata.getRepositoryInterface();
+ JpaEntityInformation, Serializable> entityInformation =
+ getEntityInformation(metadata.getDomainClass());
+
+ if (isQueryDslExecutor(repositoryInterface)) {
+ return new QueryDslJpaRepository(entityInformation, entityManager);
+ } else {
+ return new SimpleJpaRepository(entityInformation, entityManager);
+ }
}
@@ -97,7 +109,26 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
@Override
protected Class> getRepositoryBaseClass(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 QUERY_DSL_PRESENT
+ && QueryDslPredicateExecutor.class
+ .isAssignableFrom(repositoryInterface);
}
diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java
new file mode 100644
index 000000000..a282049eb
--- /dev/null
+++ b/src/main/java/org/springframework/data/jpa/repository/support/QueryDslJpaRepository.java
@@ -0,0 +1,348 @@
+/*
+ * Copyright 2008-2011 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.support;
+
+import java.io.Serializable;
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.List;
+
+import javax.persistence.EntityManager;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.domain.Sort.Order;
+import org.springframework.data.jpa.repository.QueryDslPredicateExecutor;
+import org.springframework.util.ClassUtils;
+import org.springframework.util.ReflectionUtils;
+
+import com.mysema.query.jpa.JPQLQuery;
+import com.mysema.query.jpa.impl.JPAQuery;
+import com.mysema.query.types.EntityPath;
+import com.mysema.query.types.Expression;
+import com.mysema.query.types.OrderSpecifier;
+import com.mysema.query.types.Predicate;
+import com.mysema.query.types.path.PathBuilder;
+
+
+/**
+ * QueryDsl specific extension of {@link SimpleJpaRepository} which adds
+ * implementation for {@link QueryDslPredicateExecutor}.
+ *
+ * @author Oliver Gierke
+ */
+public class QueryDslJpaRepository extends
+ SimpleJpaRepository implements QueryDslPredicateExecutor {
+
+ private final EntityManager em;
+ private final EntityPath path;
+ private final PathBuilder builder;
+
+
+ /**
+ * Creates a new {@link QueryDslJpaRepository} from the given domain class
+ * and {@link EntityManager}. This will use the
+ * {@link SimpleEntityPathResolver} to translate the given domain class into
+ * an {@link EntityPath}.
+ *
+ * @param domainClass
+ * @param entityManager
+ */
+ public QueryDslJpaRepository(JpaEntityInformation entityMetadata,
+ EntityManager entityManager) {
+
+ this(entityMetadata, entityManager, SimpleEntityPathResolver.INSTANCE);
+ }
+
+
+ /**
+ * Creates a new {@link QueryDslJpaRepository} from the given domain class
+ * and {@link EntityManager} and uses the given {@link EntityPathResolver}
+ * to translate the domain class into an {@link EntityPath}.
+ *
+ * @param domainClass
+ * @param entityManager
+ * @param resolver
+ */
+ public QueryDslJpaRepository(JpaEntityInformation entityMetadata,
+ EntityManager entityManager, EntityPathResolver resolver) {
+
+ super(entityMetadata, entityManager);
+ this.em = entityManager;
+ this.path = resolver.createPath(entityMetadata.getJavaType());
+ this.builder = new PathBuilder(path.getType(), path.getMetadata());
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.data.jpa.repository.querydsl.
+ * QueryDslSpecificationExecutor#findOne(com.mysema.query.types.Predicate)
+ */
+ public T findOne(Predicate predicate) {
+
+ return createQuery(predicate).uniqueResult(path);
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.data.jpa.repository.querydsl.
+ * QueryDslSpecificationExecutor#findAll(com.mysema.query.types.Predicate)
+ */
+ public List findAll(Predicate predicate) {
+
+ return createQuery(predicate).list(path);
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.data.jpa.repository.querydsl.
+ * QueryDslSpecificationExecutor#findAll(com.mysema.query.types.Predicate,
+ * com.mysema.query.types.OrderSpecifier>[])
+ */
+ public List findAll(Predicate predicate, OrderSpecifier>... orders) {
+
+ return createQuery(predicate).orderBy(orders).list(path);
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.data.jpa.repository.querydsl.
+ * QueryDslSpecificationExecutor#findAll(com.mysema.query.types.Predicate,
+ * org.springframework.data.domain.Pageable)
+ */
+ public Page findAll(Predicate predicate, Pageable pageable) {
+
+ JPQLQuery countQuery = createQuery(predicate);
+ JPQLQuery query = applyPagination(createQuery(predicate), pageable);
+
+ return new PageImpl(query.list(path), pageable, countQuery.count());
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.springframework.data.jpa.repository.querydsl.
+ * QueryDslSpecificationExecutor#count(com.mysema.query.types.Predicate)
+ */
+ public Long count(Predicate predicate) {
+
+ return createQuery(predicate).count();
+ }
+
+
+ /**
+ * Creates a new {@link JPQLQuery} for the given {@link Predicate}.
+ *
+ * @param predicate
+ * @return
+ */
+ private JPQLQuery createQuery(Predicate... predicate) {
+
+ return new JPAQuery(em).from(path).where(predicate);
+ }
+
+
+ /**
+ * Applies the given {@link Pageable} to the given {@link JPQLQuery}.
+ *
+ * @param query
+ * @param pageable
+ * @return
+ */
+ private JPQLQuery applyPagination(JPQLQuery query, Pageable pageable) {
+
+ if (pageable == null) {
+ return query;
+ }
+
+ query.offset(pageable.getOffset());
+ query.limit(pageable.getPageSize());
+
+ return applySorting(query, pageable.getSort());
+ }
+
+
+ /**
+ * Applies sorting to the given {@link JPQLQuery}.
+ *
+ * @param query
+ * @param sort
+ * @return
+ */
+ private JPQLQuery applySorting(JPQLQuery query, Sort sort) {
+
+ if (sort == null) {
+ return query;
+ }
+
+ for (Order order : sort) {
+ query.orderBy(toOrder(order));
+ }
+
+ return query;
+ }
+
+
+ /**
+ * Transforms a plain {@link Order} into a QueryDsl specific
+ * {@link OrderSpecifier}.
+ *
+ * @param order
+ * @return
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ private OrderSpecifier> toOrder(Order order) {
+
+ Expression