DATAJPA-454 - Fixed join creation in QueryUtils.

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.
This commit is contained in:
Oliver Gierke
2014-02-06 19:19:27 +01:00
parent 869be7eafc
commit 75d6f26572
5 changed files with 55 additions and 2 deletions

View File

@@ -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<T>) (property.hasNext() ? toExpressionRecursively(join, property.next()) : join);
} else {
@@ -481,7 +481,7 @@ public abstract class QueryUtils {
Class<? extends Annotation> associationAnnotation = ASSOCIATION_TYPES.get(attribute.getPersistentAttributeType());
if (associationAnnotation == null) {
return false;
return true;
}
Member member = attribute.getJavaMember();

View File

@@ -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;
@@ -60,6 +61,8 @@ public class User {
@Embedded private Address address;
@Lob private byte[] binaryData;
/**
* Creates a new empty instance of {@code User}.
*/
@@ -291,6 +294,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)
*

View File

@@ -1196,6 +1196,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<User> result = repository.findByBinaryData(data);
assertThat(result, hasSize(1));
assertThat(result, hasItem(firstUser));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -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<User> query = builder.createQuery(User.class);
Root<User> root = query.from(User.class);
QueryUtils.toExpressionRecursively(root, PropertyPath.from("colleaguesLastname", User.class));
assertThat(root.getJoins(), hasSize(1));
}
protected void assertNoJoinRequestedForOptionalAssociation(Root<Order> root) {
assertThat(root.getJoins(), is(empty()));
}

View File

@@ -288,4 +288,9 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
* @see DATAJPA-405
*/
List<User> findAllByOrderByLastnameAsc();
/**
* @see DATAJPA-454
*/
List<User> findByBinaryData(byte[] data);
}