DATACMNS-399 - Support query prefix to designate repository query methods.

Extended PREFIX regex in PartTree to support the "query" prefix.

Original author: @jiming
Original pull request: #56.
This commit is contained in:
Thomas Darimont
2013-11-13 17:55:49 +01:00
committed by Oliver Gierke
parent ba89dc9bb4
commit eddb3323de
3 changed files with 16 additions and 3 deletions

View File

@@ -316,7 +316,7 @@ interface UserRepository extends MyBaseRepository<User, Long> {
<para>The query builder mechanism built into Spring Data repository
infrastructure is useful for building constraining queries over
entities of the repository. The mechanism strips the prefixes
<code>find…By</code>, <code>read…By</code>, and <code>get…By</code>
<code>find…By</code>, <code>read…By</code>, <code>query…By</code>, and <code>get…By</code>
from the method and starts parsing the rest of it. The introducing
clause can contain further expressions such as a <code>Distinct</code>
to set a distinct flag on the query to be created. However, the first

View File

@@ -38,7 +38,7 @@ import org.springframework.util.StringUtils;
*/
public class PartTree implements Iterable<OrPart> {
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(find|read|get|count)(\\p{Lu}.*?)??By");
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(find|read|get|count|query)(\\p{Lu}.*?)??By");
/*
* We look for a pattern of: keyword followed by

View File

@@ -44,7 +44,7 @@ import org.springframework.data.repository.query.parser.PartTree.OrPart;
*/
public class PartTreeUnitTests {
private String[] PREFIXES = { "find", "read", "get" };
private String[] PREFIXES = { "find", "read", "get", "query" };
@Test(expected = IllegalArgumentException.class)
public void rejectsNullSource() throws Exception {
@@ -417,6 +417,19 @@ public class PartTreeUnitTests {
assertThat(tree.isCountProjection(), is(true));
}
/**
* @see DATACMNS-399
*/
@Test
public void queryPrefixShouldBeSupportedInRepositoryQueryMethods() {
PartTree tree = new PartTree("queryByFirstnameAndLastname", User.class);
Iterable<Part> parts = tree.getParts();
assertThat(parts, hasItem(part("firstname")));
assertThat(parts, hasItem(part("lastname")));
}
/**
* @see DATACMNS-303
*/