This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Data LDAP 3.4.4!spring-doc.cn

Query Methods

Most of the data access operations you usually trigger on a repository result in a query being run against the LDAP directory. Defining such a query is a matter of declaring a method on the repository interface, as the following example shows:spring-doc.cn

PersonRepository with query methods
interface PersonRepository extends PagingAndSortingRepository<Person, String> {

    List<Person> findByLastname(String lastname);                            (1)

    List<Person> findByLastnameFirstname(String lastname, String firstname); (2)
}
1 The method shows a query for all people with the given lastname. The query is derived by parsing the method name for constraints that can be concatenated with And and Or. Thus, the method name results in a query expression of (&(objectclass=person)(lastname=lastname)).
2 The method shows a query for all people with the given lastname and firstname. The query is derived by parsing the method name. Thus, the method name results in a query expression of (&(objectclass=person)(lastname=lastname)(firstname=firstname)).

The following table provides samples of the keywords that you can use with query methods:spring-doc.cn

Table 1. Supported keywords for query methods
Keyword Sample Logical result

LessThanEqualspring-doc.cn

findByAgeLessThanEqual(int age)spring-doc.cn

(attribute⇐age)spring-doc.cn

GreaterThanEqualspring-doc.cn

findByAgeGreaterThanEqual(int age)spring-doc.cn

(attribute>=age)spring-doc.cn

IsNotNull, NotNullspring-doc.cn

findByFirstnameNotNull()spring-doc.cn

(firstname=*)spring-doc.cn

IsNull, Nullspring-doc.cn

findByFirstnameNull()spring-doc.cn

(!(firstname=*))spring-doc.cn

Likespring-doc.cn

findByFirstnameLike(String name)spring-doc.cn

(firstname=name)spring-doc.cn

NotLike, IsNotLikespring-doc.cn

findByFirstnameNotLike(String name)spring-doc.cn

(!(firstname=name*))spring-doc.cn

StartingWithspring-doc.cn

findByStartingWith(String name)spring-doc.cn

(firstname=name*)spring-doc.cn

EndingWithspring-doc.cn

findByFirstnameLike(String name)spring-doc.cn

(firstname=*name)spring-doc.cn

Containingspring-doc.cn

findByFirstnameLike(String name)spring-doc.cn

(firstname=*name*)spring-doc.cn

(No keyword)spring-doc.cn

findByFirstname(String name)spring-doc.cn

(Firstname=name)spring-doc.cn

Notspring-doc.cn

findByFirstnameNot(String name)spring-doc.cn

(!(Firstname=name))spring-doc.cn

Using @Query

If you need to use a custom query that can’t be derived from the method name, you can use the @Query annotation to define the query. As queries are tied to the Java method that runs them, you can actually bind parameters to be passed to the query.spring-doc.cn

The following example shows a query created with the @Query annotation:spring-doc.cn

Example 1. Declare query at the query method using @Query
interface PersonRepository extends LdapRepository<Person, Long> {

  @Query("(&(employmentType=*)(!(employmentType=Hired))(mail=:emailAddress))")
  Person findEmployeeByEmailAddress(String emailAddress);

}
Spring Data supports named (parameter names prefixed with :) and positional parameter binding (in the form of zero-based ?0). We recommend using named parameters for easier readability. Also, using positional parameters makes query methods a little error-prone when refactoring regarding the parameter position.

Parameter Encoding

Query parameters of String-based queries are encoded according to RFC2254. This can lead to undesired escaping of certain characters. You can specify your own encoder through the @LdapEncode annotation that defines which LdapEncoder to use.spring-doc.cn

@LdapEncode applies to individual parameters of a query method. It is not applies for derived queries or Value Expressions (SpEL, Property Placeholders).spring-doc.cn

Example 2. Declare a custom LdapEncoder for a query method
interface PersonRepository extends LdapRepository<Person, Long> {

  @Query("(&(employmentType=*)(!(employmentType=Hired))(firstName=:firstName))")
  Person findEmployeeByFirstNameLike(@LdapEncode(MyLikeEncoder.class) String firstName);

}

Using SpEL Expressions

Spring Data allows you to use SpEL expressions in your query methods. SpEL expressions are part of Spring Data’s Value Expressions support. SpEL expressions can be used to manipulate query method arguments as well as to invoke bean methods. Method arguments can be accessed by name or index as demonstrated in the following example.spring-doc.cn

Example 3. Using SpEL expressions in Repository Query Methods
@Query("(&(firstName=?#{[0]})(mail=:?#{principal.emailAddress}))")
List<Person> findByFirstnameAndCurrentUserWithCustomQuery(String firstname);
Values provided by SpEL expressions are not escaped according to RFC2254. You have to ensure that the values are properly escaped if needed. Consider using Spring Ldap’s org.springframework.ldap.support.LdapEncoder helper class.

Using Property Placeholders

Property Placeholders (see Value Expressions) can help to easily customize your queries based on configuration properties from Spring’s Environment. These are useful for queries that need to be customized based on the environment or configuration.spring-doc.cn

Example 4. Using Property Placeholders in Repository Query Methods
@Query("(&(firstName=?0)(stage=:?${myapp.stage:dev}))")
List<Person> findByFirstnameAndStageWithCustomQuery(String firstname);
Values provided by Property Placeholders are not escaped according to RFC2254. You have to ensure that the values are properly escaped if needed. Consider using Spring Ldap’s org.springframework.ldap.support.LdapEncoder helper class.