DATAJPA-132 - Implemented handling of True and False keywords on query creation.

Query derivation mechanism now supports True and False as keywords in finder methods:

class User {
  boolean active;
}

interface UserRepository<User, Long> {
  List<User> findByActiveTrue() ;
}
This commit is contained in:
Oliver Gierke
2011-12-05 14:38:39 +01:00
parent d3bced0f86
commit 42b60cf8d4
5 changed files with 77 additions and 2 deletions

View File

@@ -271,6 +271,22 @@
<entry><code>… where x.age not in ?1</code></entry>
</row>
<row>
<entry><code>True</code></entry>
<entry><code>findByActiveTrue()</code></entry>
<entry><code>… where x.active = true</code></entry>
</row>
<row>
<entry><code>False</code></entry>
<entry><code>findByActiveFalse()</code></entry>
<entry><code>… where x.active = false</code></entry>
</row>
</tbody>
</tgroup>
</table><note>

View File

@@ -176,8 +176,7 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<Object>,
return getTypedPath(root, part, Comparable.class);
}
private <T> Expression<? extends T> getTypedPath(Root<?> root, Part part, Class<T> type) {
private <T> Expression<T> getTypedPath(Root<?> root, Part part, Class<T> type) {
return toExpressionRecursively(root, part.getProperty());
}
@@ -326,6 +325,10 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<Object>,
Expression<String> parameterExpression = upperIfIgnoreCase(provider.next(String.class));
Predicate like = builder.like(propertyExpression, parameterExpression);
return part.getType() == Type.LIKE ? like : like.not();
case TRUE:
return builder.isTrue(getTypedPath(root, part, Boolean.class));
case FALSE:
return builder.isFalse(getTypedPath(root, part, Boolean.class));
case SIMPLE_PROPERTY:
return builder.equal(upperIfIgnoreCase(path), upperIfIgnoreCase(provider.next()));
case NEGATING_SIMPLE_PROPERTY:

View File

@@ -46,6 +46,7 @@ public class User {
private String firstname;
private String lastname;
private int age;
private boolean active;
@Column(nullable = false, unique = true)
private String emailAddress;
@@ -81,6 +82,7 @@ public class User {
this.firstname = firstname;
this.lastname = lastname;
this.emailAddress = emailAddress;
this.active = true;
}
/**
@@ -173,6 +175,20 @@ public class User {
this.emailAddress = emailAddress;
}
/**
* @param active the active to set
*/
public void setActive(boolean active) {
this.active = active;
}
/**
* @return the active
*/
public boolean isActive() {
return active;
}
/**
* Returns the user's roles.
*

View File

@@ -694,6 +694,36 @@ public class UserRepositoryTests {
assertThat(result.size(), is(1));
}
/**
* @see DATAJPA-132
*/
@Test
public void executesFinderWithTrueKeywordCorrectly() {
flushTestUsers();
firstUser.setActive(false);
repository.save(firstUser);
List<User> result = repository.findByActiveTrue();
assertThat(result.size(), is(2));
assertThat(result, hasItems(secondUser, thirdUser));
}
/**
* @see DATAJPA-132
*/
@Test
public void executesFinderWithFalseKeywordCorrectly() {
flushTestUsers();
firstUser.setActive(false);
repository.save(firstUser);
List<User> result = repository.findByActiveFalse();
assertThat(result.size(), is(1));
assertThat(result, hasItem(firstUser));
}
private void flushTestUsers() {
firstUser = repository.save(firstUser);

View File

@@ -197,4 +197,14 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
*/
@Query(value = "SELECT * FROM User WHERE lastname = ?1", nativeQuery = true)
List<User> findNativeByLastname(String lastname);
/**
* @see DATAJPA-132
*/
List<User> findByActiveTrue();
/**
* @see DATAJPA-132
*/
List<User> findByActiveFalse();
}