diff --git a/src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java b/src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java index 1fa518568..a8c0edf3d 100644 --- a/src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java +++ b/src/main/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilder.java @@ -129,7 +129,7 @@ public class QueryByExamplePredicateBuilder { if (!(from instanceof From)) { throw new JpaSystemException(new IllegalArgumentException( - String.format("Unexpected path type for %s. Found % where From.class was expected.", currentPath, from))); + String.format("Unexpected path type for %s. Found %s where From.class was expected.", currentPath, from))); } PathNode node = currentNode.add(attribute.getName(), attributeValue); diff --git a/src/test/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilderUnitTests.java b/src/test/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilderUnitTests.java index 3ddf152da..8294b290d 100644 --- a/src/test/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilderUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/convert/QueryByExamplePredicateBuilderUnitTests.java @@ -37,8 +37,11 @@ import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.Type; +import org.hamcrest.core.IsInstanceOf; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.mockito.Matchers; import org.mockito.Mock; @@ -46,6 +49,8 @@ import org.mockito.runners.MockitoJUnitRunner; import org.springframework.util.ObjectUtils; /** + * Unit tests for {@link QueryByExamplePredicateBuilder}. + * * @author Christoph Strobl * @author Mark Paluch */ @@ -70,6 +75,8 @@ public class QueryByExamplePredicateBuilderUnitTests { SingularAttribute personSkillAttribute; SingularAttribute personAddressAttribute; + public @Rule ExpectedException exception = ExpectedException.none(); + @Before public void setUp() { @@ -78,7 +85,7 @@ public class QueryByExamplePredicateBuilderUnitTests { String.class); personAgeAttribute = new SingluarAttributeStub("age", PersistentAttributeType.BASIC, Long.class); personFatherAttribute = new SingluarAttributeStub("father", PersistentAttributeType.MANY_TO_ONE, - Person.class); + Person.class, personEntityType); personSkillAttribute = new SingluarAttributeStub("skill", PersistentAttributeType.MANY_TO_ONE, Skill.class); personAddressAttribute = new SingluarAttributeStub("address", PersistentAttributeType.EMBEDDED, @@ -150,6 +157,23 @@ public class QueryByExamplePredicateBuilderUnitTests { verify(cb, times(1)).equal(any(Expression.class), eq("foo")); } + /** + * @see DATAJPA-937 + */ + @Test + public void unresolvableNestedAssociatedPathShouldFail() { + + Person p = new Person(); + Person father = new Person(); + father.father = new Person(); + p.father = father; + + exception.expectCause(IsInstanceOf. instanceOf(IllegalArgumentException.class)); + exception.expectMessage("Unexpected path type"); + + QueryByExamplePredicateBuilder.getPredicate(root, cb, of(p)); + } + /** * @see DATAJPA-218 */ @@ -193,12 +217,19 @@ public class QueryByExamplePredicateBuilderUnitTests { private String name; private PersistentAttributeType attributeType; - private Class type; + private Class javaType; + private Type type; public SingluarAttributeStub(String name, - javax.persistence.metamodel.Attribute.PersistentAttributeType attributeType, Class type) { + javax.persistence.metamodel.Attribute.PersistentAttributeType attributeType, Class javaType) { + this(name, attributeType, javaType, null); + } + + public SingluarAttributeStub(String name, + javax.persistence.metamodel.Attribute.PersistentAttributeType attributeType, Class javaType, Type type) { this.name = name; this.attributeType = attributeType; + this.javaType = javaType; this.type = type; } @@ -219,7 +250,7 @@ public class QueryByExamplePredicateBuilderUnitTests { @Override public Class getJavaType() { - return type; + return javaType; } @Override @@ -245,7 +276,7 @@ public class QueryByExamplePredicateBuilderUnitTests { @Override public Class getBindableJavaType() { - return type; + return javaType; } @Override @@ -265,7 +296,7 @@ public class QueryByExamplePredicateBuilderUnitTests { @Override public Type getType() { - return null; + return type; } }