diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java index 6b64b85d8..bb1246833 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java @@ -1,5 +1,5 @@ /* - * Copyright 2008-2015 the original author or authors. + * Copyright 2008-2016 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. @@ -15,6 +15,7 @@ */ package org.springframework.data.jpa.repository.query; +import static org.springframework.data.jpa.domain.AbstractPersistable_.*; import static org.springframework.data.jpa.repository.query.QueryUtils.*; import static org.springframework.data.repository.query.parser.Part.Type.*; @@ -22,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; +import java.util.Set; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; @@ -30,6 +32,7 @@ import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.persistence.criteria.Selection; +import javax.persistence.metamodel.SingularAttribute; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.query.ParameterMetadataProvider.ParameterMetadata; @@ -45,6 +48,7 @@ import org.springframework.util.Assert; * Query creator to create a {@link CriteriaQuery} from a {@link PartTree}. * * @author Oliver Gierke + * @author Mark Paluch */ public class JpaQueryCreator extends AbstractQueryCreator, Predicate> { @@ -53,6 +57,7 @@ public class JpaQueryCreator extends AbstractQueryCreator query; private final ParameterMetadataProvider provider; private final ReturnedType returnedType; + private final PartTree tree; /** * Create a new {@link JpaQueryCreator}. @@ -66,6 +71,7 @@ public class JpaQueryCreator extends AbstractQueryCreator criteriaQuery = createCriteriaQuery(builder, type); @@ -87,7 +93,7 @@ public class JpaQueryCreator extends AbstractQueryCreator typeToRead = type.getTypeToRead(); - return typeToRead == null ? builder.createTupleQuery() : builder.createQuery(typeToRead); + return (typeToRead == null || tree.isExistsProjection()) ? builder.createTupleQuery() : builder.createQuery(typeToRead); } /** @@ -162,6 +168,24 @@ public class JpaQueryCreator extends AbstractQueryCreator id = root.getModel().getId(root.getModel().getIdType().getJavaType()); + query = query.multiselect(root.get((SingularAttribute) id).alias(id.getName())); + } else { + + List> selections = new ArrayList>(); + + Set> idClassAttributes = (Set>) root.getModel().getIdClassAttributes(); + + for (SingularAttribute attribute : idClassAttributes) { + selections.add(root.get((SingularAttribute) attribute).alias(attribute.getName())); + } + selections.add(root.get((SingularAttribute) id).alias(id.getName())); + query = query.multiselect(selections); + } } else { query = query.select((Root) root); } @@ -172,7 +196,7 @@ public class JpaQueryCreator extends AbstractQueryCreator[] parameterTypes = new Class[values.length]; @@ -173,6 +202,8 @@ public class PartTreeJpaQueryIntegrationTests { User findByIdAllIgnoringCase(Integer id); + boolean existsByFirstname(String firstname); + List findByCreatedAtAfter(@Temporal(TemporalType.TIMESTAMP) @Param("refDate") Date refDate); } } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithEmbeddedId.java b/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithEmbeddedId.java index edf15484f..3454815fe 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithEmbeddedId.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithEmbeddedId.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 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. @@ -30,6 +30,7 @@ import com.querydsl.core.types.Predicate; * Demonstrates the support for composite primary keys with {@code @EmbeddedId}. * * @author Thomas Darimont + * @author Mark Paluch */ @Lazy public interface EmployeeRepositoryWithEmbeddedId @@ -37,4 +38,9 @@ public interface EmployeeRepositoryWithEmbeddedId QueryDslPredicateExecutor { List findAll(Predicate predicate, OrderSpecifier... orders); + + /** + * @see DATAJPA-920 + */ + boolean existsByName(String name); } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithIdClass.java b/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithIdClass.java index f5439053e..a5228e523 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithIdClass.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/EmployeeRepositoryWithIdClass.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2015 the original author or authors. + * Copyright 2013-2016 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. @@ -30,10 +30,16 @@ import com.querydsl.core.types.Predicate; * Demonstrates the support for composite primary keys with {@code @IdClass}. * * @author Thomas Darimont + * @author Mark Paluch */ @Lazy public interface EmployeeRepositoryWithIdClass extends JpaRepository, QueryDslPredicateExecutor { List findAll(Predicate predicate, OrderSpecifier... orders); + + /** + * @see DATAJPA-920 + */ + boolean existsByName(String name); } diff --git a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java index b01d19ee1..e9f7c2c5f 100644 --- a/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java +++ b/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java @@ -282,6 +282,11 @@ public interface UserRepository */ int countUsersByFirstname(String firstname); + /** + * @see DATAJPA-920 + */ + boolean existsByLastname(String lastname); + /** * @see DATAJPA-391 */