Skip to main content

Spring Data Methods

Spring Data Aerospike supports defining queries by method name in the Repository interface and Spring will generate the necessary bodies. The format of the name is fairly flexible, comprising of a verb and a criteria.

Some of the verbs include find, query, read, get, count and delete. For example: countByLastName or findByFirstName.

KeywordSampleSnippet
AndfindByLastNameAndFirstName...where x.lastName = ? and x.firstName = ?
OrfindByLastNameOrFirstName...where x.lastName = ? or x.firstName = ?
Is, EqualsfindByLastName, findByLastNameIs, findByLastNameEquals...where x.lastName = ?
BetweenfindByDateOfBirthBetween...where x.dateOfBirth between ? and ?
LessThanfindByAgeLessThan...where x.age < ?
LessThanEqualfindByAgeLessThanEqual...where x.age <= ?
GreaterThanfindByAgeGreaterThan...where x.age > ?
GreaterThanEqualfindByAgeGreaterThanEqual...where x.age >= ?
AfterfindByDateOfBirthAfter...where x.dateOfBirth > ?
BeforefindByDateOfBirthBefore...where x.dateOfBirth < ?
LikefindByLastNameLike...where x.lastName like ?
StartingWithfindByLastNameStartingWith...where x.lastName like 'abc%'
EndingWithfindByLastNameEndingWith...where x.lastName like '%abc'
ContainingfindByLastNameContaining...where x.lastName like '%abc%
OrderByfindByLastNameOrderByFirstNameDesc...where x.lastName = ? order by x.firstName desc
NotfindByLastNameNot...where x.lastName <> ?
InfindByLastNameIn(Collection<String>)...where x.lastName in ?
TruefindByEnabledTrue()...where x.enabled = true
FalsefindByOptOutFalse()...where x.optOut = false
IgnoreCasefindByLastNameIgnoreCase...where UPPER(x.lastName) = UPPER(?)

Other useful special modifiers:

  • Distinct: Allows selection of unique results, for example findDistinctByLastName. Distinct is not supported by Spring Data Aerospike.
  • First or Top: Limit the number of results returned, for example findFirstByLastName or findTop20ByLastName.

An example of an interface with several query methods is:

public interface PersonRepository extends AerospikeRepository<Person, Long> {
public List<Person> findByLastName(String lastName);
public List<Person> findByLastNameContaining(String lastName);
public List<Person> findByLastNameStartingWith(String lastName);
public List<Person> findByLastNameAndFirstNameContaining(String lastName, String firstName);
public List<Person> findByAgeBetween(long startAge, long endAge);
public Optional<Person> findById(Long id);
}

Note that Java Dates are stored as type long in the database so search method should access them as if they are of type long. So if the class has a DateOfBirth field, instead of

public List<Person> findByDateOfBirthBetween(Date startDate, Date endDate);

there might be a method like:

public List<Person> findByDateOfBirthBetween(long startDate, long endDate);