From 30bd5b541617c6a560399ee211e4f8af3b8fd66b Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Thu, 6 Feb 2014 19:19:27 +0100 Subject: [PATCH] DATAJPA-454 - Fixed join creation in QueryUtils. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QueryUtils now only creates a join for collection properties that are explicitly annotated with an @ManyTo… annotation. This allows collection like properties like byte[] be referred to as non-collection property and thus not trigger a join when a derived query is created. --- .../data/jpa/repository/query/QueryUtils.java | 4 ++-- .../data/jpa/domain/sample/User.java | 17 +++++++++++++++++ .../jpa/repository/UserRepositoryTests.java | 16 ++++++++++++++++ .../query/QueryUtilsIntegrationTests.java | 15 +++++++++++++++ .../jpa/repository/sample/UserRepository.java | 5 +++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java index e7f311e42..e2aca56f5 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java @@ -450,7 +450,7 @@ public abstract class QueryUtils { propertyPathModel = from.get(property.getSegment()).getModel(); } - if (property.isCollection() || requiresJoin(propertyPathModel)) { + if (requiresJoin(propertyPathModel)) { Join join = getOrCreateJoin(from, property.getSegment()); return (Expression) (property.hasNext() ? toExpressionRecursively(join, property.next()) : join); } else { @@ -481,7 +481,7 @@ public abstract class QueryUtils { Class associationAnnotation = ASSOCIATION_TYPES.get(attribute.getPersistentAttributeType()); if (associationAnnotation == null) { - return false; + return true; } Member member = attribute.getJavaMember(); diff --git a/src/test/java/org/springframework/data/jpa/domain/sample/User.java b/src/test/java/org/springframework/data/jpa/domain/sample/User.java index 0a99c7678..47347edcd 100644 --- a/src/test/java/org/springframework/data/jpa/domain/sample/User.java +++ b/src/test/java/org/springframework/data/jpa/domain/sample/User.java @@ -26,6 +26,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.Lob; import javax.persistence.ManyToMany; import javax.persistence.ManyToOne; import javax.persistence.NamedQuery; @@ -66,6 +67,8 @@ public class User { @Embedded private Address address; + @Lob private byte[] binaryData; + /** * Creates a new empty instance of {@code User}. */ @@ -298,6 +301,20 @@ public class User { this.address = address; } + /** + * @param binaryData the binaryData to set + */ + public void setBinaryData(byte[] binaryData) { + this.binaryData = binaryData; + } + + /** + * @return the binaryData + */ + public byte[] getBinaryData() { + return binaryData; + } + /* * (non-Javadoc) * diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index 4fa5aa49a..9bb734d1e 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -1154,6 +1154,22 @@ public class UserRepositoryTests { assertThat(page.getContent().get(3), is(thirdUser)); } + /** + * @see DATAJPA-454 + */ + @Test + public void findsUserByBinaryDataReference() throws Exception { + + byte[] data = "Woho!!".getBytes("UTF-8"); + firstUser.setBinaryData(data); + + flushTestUsers(); + + List result = repository.findByBinaryData(data); + assertThat(result, hasSize(1)); + assertThat(result, hasItem(firstUser)); + } + private Page executeSpecWithSort(Sort sort) { flushTestUsers(); diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java index 682b4c166..cc979cc2f 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsIntegrationTests.java @@ -90,6 +90,21 @@ public class QueryUtilsIntegrationTests { QueryUtils.toExpressionRecursively(root, PropertyPath.from("customer", Order.class)); } + /** + * @see DATAJPA-454 + */ + @Test + public void createsJoingToTraverseCollectionPath() { + + CriteriaBuilder builder = em.getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery(User.class); + Root root = query.from(User.class); + + QueryUtils.toExpressionRecursively(root, PropertyPath.from("colleaguesLastname", User.class)); + + assertThat(root.getJoins(), hasSize(1)); + } + protected void assertNoJoinRequestedForOptionalAssociation(Root root) { assertThat(root.getJoins(), is(empty())); } 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 77a0e3075..22fe57e4d 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 @@ -275,4 +275,9 @@ public interface UserRepository extends JpaRepository, JpaSpecifi * @see DATAJPA-405 */ List findAllByOrderByLastnameAsc(); + + /** + * @see DATAJPA-454 + */ + List findByBinaryData(byte[] data); }