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, consisting of a verb and a criteria.
Some of the verbs include find
, query
, read
, get
, count
and delete
. For example: countByLastName
or findByFirstName
.
Simple property repository queries
Keyword | Repository query sample | Snippet | Notes |
---|---|---|---|
Is, Equals or no keyword | findByLastName(String lastName) | …where x.lastName = ? | |
Not, IsNot | findByLastNameNot(String lastName) | …where x.lastName <> ? | |
True, isTrue | findByEnabledTrue() | …where x.enabled = true | |
False, isFalse | findByEnabledFalse() | …where x.enabled = false | |
In, IsIn | findByLastNameIn(Collection<String>) | …where x.lastName in ? | |
NotIn, IsNotIn | findByLastNameNotIn(Collection<String>) | …where x.lastName not in ? | |
Null, IsNull | findByEmailAddressIsNull() | …where x.emailAddress = null or x.emailAddress does not exist | The same as “does not exist”. Objects and fields exist in Aerospike when their value is not equal to null. |
Exists NotNull, IsNotNull | findByEmailAddressExists() , findByEmailAddressNotNull() | …where x.emailAddress != null | ”Exists” and “IsNotNull” represent the same functionality and can be used interchangeably. Objects and fields exist in Aerospike when their value is not equal to null. |
LessThan, IsLessThan | findByAgeLessThan(int age) , findByFirstNameLessThan(String string) | …where x.age < ?, where x.firstName < ? | Strings are compared by order of each byte, assuming they have UTF-8 encoding. See information about ordering. |
LessThanEqual, IsLessThanEqual | findByAgeLessThanEqual(int age) , findByFirstNameLessThanEqual(String string) | …where x.age <= ?, …where x.firstName <= ? | Strings are compared by order of each byte, assuming they have UTF-8 encoding. See information about ordering. |
GreaterThan, IsGreaterThan | findByAgeGreaterThan(int age) , findByFirstNameGreaterThan(String string) | …where x.age > ?, …where x.firstName > ? | Strings are compared by order of each byte, assuming they have UTF-8 encoding. See information about ordering. |
GreaterThanEqual, IsGreaterThanEqual | findByAgeGreaterThanEqual(int age) , findByFirstNameGreaterThanEqual(String string) | …where x.age >= ?, …where x.firstName >= ? | Strings are compared by order of each byte, assuming they have UTF-8 encoding. See information about ordering. |
Between, IsBetween | findByAgeBetween(int lowerLimit, int upperLimit) , findByFirstNameBetween(String lowerLimit, String upperLimit) | …where x.age between ? and ?, …where x.firstName between ? and ? | Strings are compared by order of each byte, assuming they have UTF-8 encoding. See information about ordering. |
Before, IsBefore | findByDateOfBirthBefore(Date date) | …where x.dateOfBirth < ? | |
After, IsAfter | findByDateOfBirthAfter(Date date) | …where x.dateOfBirth > ? | |
Like, IsLike, MatchesRegex | findByLastNameLike(String lastNameRegex) | …where x.lastName like ? | |
StartingWith, IsStartingWith, StartsWith | findByLastNameStartingWith(String string) | …where x.lastName like ‘abc%‘ | |
EndingWith, IsEndingWith, EndsWith | findByLastNameEndingWith(String string) | …where x.lastName like ‘%abc’ | |
Containing, IsContaining, Contains | findByLastNameContaining(String substring) | …where x.lastName like ‘%abc% | |
NotContaining, IsNotContaining, NotContains | findByLastNameNotContaining(String substring) | …where x.lastName not like ‘%abc%‘ | |
And | findByLastNameAndFirstName(String lastName, String firstName) | …where x.lastName = ? and x.firstName = ? | |
Or | findByLastNameOrFirstName(String lastName, String firstName) | …where x.lastName = ? or x.firstName = ? |
Collection repository queries
Keyword | Repository query sample | Snippet | Notes |
---|---|---|---|
Is, Equals or no keyword | findByStringList(Collection<String> stringList) | …where x.lastName = ? | |
Not, IsNot | findByStringListNot(Collection<String> stringList) | …where x.lastName <> ? | |
In | findByStringListIn(Collection<Collection<String>>) | …where x.lastName in ? | Find records where stringList bin value equals one of the collections in the given argument. |
Not In | findByStringListNotIn(Collection<Collection<String>>) | …where x.lastName not in ? | Find records where stringList bin value is not equal to any of the collections in the given argument. |
Null, IsNull | findByStringListIsNull() | …where x.emailAddress = null or x.emailAddress does not exist | The same as “does not exist”. Objects and fields exist in Aerospike when their value is not equal to null. |
Exists NotNull, IsNotNull | findByStringListExists() , findByStringListNotNull() | …where x.emailAddress != null | ”Exists” and “IsNotNull” represent the same functionality and can be used interchangeably. Objects and fields exist in Aerospike when their value is not equal to null. |
LessThan, IsLessThan | findByStringListLessThan(Collection<String> stringList) | …where x.age < ?, where x.firstName < ? | Find records where stringList bin value has fewer elements or has a corresponding element lower in ordering than in the given argument. See information about ordering. |
LessThanEqual, IsLessThanEqual | findByStringListLessThanEqual(Collection<String> stringList) | …where x.age <= ?, …where x.firstName <= ? | Find records where stringList bin value has smaller or the same amount of elements or has each corresponding element lower in ordering or the same as in the given argument. See information about ordering. |
GreaterThan, IsGreaterThan | findByStringListGreaterThan(Collection<String> stringList) | …where x.age > ?, …where x.firstName > ? | Find records where stringList bin value has more elements or has a corresponding element higher in ordering than in the given argument. See information about ordering. |
GreaterThanEqual, IsGreaterThanEqual | findByStringListGreaterThanEqual(Collection<String> stringList) | …where x.age >= ?, …where x.firstName >= ? | Find records where stringList bin value has larger or the same amount of elements or has each corresponding element higher in ordering or the same as in the given argument. See information about ordering. |
Between, IsBetween | findByStringListBetween(Collection<String> lowerLimit, Collection<String> upperLimit) | …where x.age between ? and ?, …where x.firstName between ? and ? | Find records where stringList bin value is in the range between the given arguments. See information about ordering. |
Containing, IsContaining, Contains | findByStringListContaining(String string) | …where x.lastName like ‘%abc% | |
NotContaining, IsNotContaining, NotContains | findByStringListNotContaining(String string) | …where x.lastName not like ‘%abc%‘ | |
And | findByStringListAndIntList(Collection<String> stringList, Collection<Integer> intList) | …where x.lastName = ? and x.firstName = ? | |
Or | findByStringListOrIntList(Collection<String> stringList, Collection<Integer> intList) | …where x.lastName = ? or x.firstName = ? |
Map repository queries
Keyword | Repository query sample | Snippet | Notes |
---|---|---|---|
Is, Equals or no keyword | findByStringMap(Map<String, String> stringMap) | …where x.lastName = ? | |
Not, IsNot | findByStringMapNot(Map<String, String> stringMap) | …where x.lastName <> ? | |
In | findByStringMapIn(Collection<Map<String, String>>) | …where x.lastName in ? | Find records where stringMap bin value equals one of the maps in the given argument. |
Not In | findByStringMapNotIn(Collection<Map<String, String>>) | …where x.lastName not in ? | Find records where stringMap bin value is not equal to any of the maps in the given argument. |
Null, IsNull | findByStringMapIsNull() | …where x.emailAddress = null or x.emailAddress does not exist | The same as “does not exist”. Objects and fields exist in Aerospike when their value is not equal to null. |
Exists NotNull, IsNotNull | findByStringMapExists() , findByStringMapNotNull() | …where x.emailAddress != null | ”Exists” and “IsNotNull” represent the same functionality and can be used interchangeably. Objects and fields exist in Aerospike when their value is not equal to null. |
LessThan, IsLessThan | findByStringMapLessThan(Map<String, String> stringMap) | …where x.age < ?, where x.firstName < ? | Find records where stringMap bin value has fewer elements or has a corresponding element lower in ordering than in the given argument. See information about ordering. |
LessThanEqual, IsLessThanEqual | findByStringMapLessThanEqual(Map<String, String> stringMap) | …where x.age <= ?, …where x.firstName <= ? | Find records where stringMap bin value has smaller or the same amount of elements or has each corresponding element lower in ordering or the same as in the given argument. See information about ordering. |
GreaterThan, IsGreaterThan | findByStringMapGreaterThan(Map<String, String> stringMap) | …where x.age > ?, …where x.firstName > ? | Find records where stringMap bin value has more elements or has a corresponding element higher in ordering than in the given argument. See information about ordering. |
GreaterThanEqual, IsGreaterThanEqual | findByStringMapGreaterThanEqual(Map<String, String> stringMap) | …where x.age >= ?, …where x.firstName >= ? | Find records where stringMap bin value has larger or the same amount of elements or has each corresponding element higher in ordering or the same as in the given argument. See information about ordering. |
Between, IsBetween | findByStringMapBetween(Map<String, String> lowerLimit, Map<String, String> upperLimit) | …where x.age between ? and ?, …where x.firstName between ? and ? | Find records where stringMap bin value is in the range between the given arguments. See information about ordering. |
Containing, IsContaining, Contains | findByStringMapContaining(AerospikeQueryCriterion criterion, String string) , findByStringMapContaining(AerospikeQueryCriterion criterionPair, String string, String value) | …where x.lastName like ‘%abc% | Find records where stringMap bin value (which is a Map) contains key “key1”: findByStringMapContaining(KEY, "key1") . Find records where stringMap bin value (which is a Map) contains value “value1”: findByStringMapContaining(VALUE, "value1") . Find records where stringMap bin value (which is a Map) contains key “key1” with the value “value1”: findByStringMapContaining(KEY_VALUE_PAIR, "key1", "value1") |
NotContaining, IsNotContaining, NotContains | findByStringNameNotContaining(AerospikeQueryCriterion criterion, String string) | …where x.lastName not like ‘%abc%‘ | findByStringMapNotContaining(KEY, "key1") , findByStringMapNotContaining(VALUE, "value1") , findByStringMapNotContaining(KEY_VALUE_PAIR, "key1", "value1") |
And | findByStringMapAndIntMap(Map<String, String> stringMap, Map<Integer, Integer> intMap) | …where x.lastName = ? and x.firstName = ? | |
Or | findByStringMapOrIntMap(Map<String, String> stringMap, Map<Integer, Integer> intList) | …where x.lastName = ? or x.firstName = ? |
POJO (Plain Old Java Object) repository queries
Keyword | Repository query sample | Snippet | Notes |
---|---|---|---|
Is, Equals or no keyword | findByAddress(Address address) | …where x.lastName = ? | |
Not, IsNot | findByAddressNot(Address address) | …where x.lastName <> ? | |
In | findByAddressIn(Collection<Address>) | …where x.lastName in ? | Find records where address bin value equals one of the Address objects in the given argument. |
Not In | findByAddressNotIn(Collection<Address>) | …where x.lastName not in ? | Find records where address bin value is not equal to any of the Address objects in the given argument. |
Null, IsNull | findByAddressIsNull() | …where x.emailAddress = null or x.emailAddress does not exist | The same as “does not exist”. Objects and fields exist in Aerospike when their value is not equal to null. |
Exists NotNull, IsNotNull | findByAddressExists() , findByAddressNotNull() | …where x.emailAddress != null | ”Exists” and “IsNotNull” represent the same functionality and can be used interchangeably. Objects and fields exist in Aerospike when their value is not equal to null. |
LessThan, IsLessThan | findByAddressLessThan(Address address) | …where x.age < ?, where x.firstName < ? | Find records where address bin value (POJOs are stored in Aerospike as maps) has fewer elements or has a corresponding element lower in ordering than in the given argument. See information about ordering. |
LessThanEqual, IsLessThanEqual | findByAddressLessThanEqual(Address address) | …where x.age <= ?, …where x.firstName <= ? | Find records where address bin value (POJOs are stored in Aerospike as maps) has smaller or the same amount of elements or has each corresponding element lower in ordering or the same as in the given argument. See information about ordering. |
GreaterThan, IsGreaterThan | findByAddressGreaterThanEqual(Address address) | …where x.age > ?, …where x.firstName > ? | Find records where address bin value (POJOs are stored in Aerospike as maps) has more elements or has a corresponding element higher in ordering than in the given argument. See information about ordering. |
GreaterThanEqual, IsGreaterThanEqual | findByStringMapGreaterThanEqual(Map<String, String> stringMap) | …where x.age >= ?, …where x.firstName >= ? | Find records where address bin value (POJOs are stored in Aerospike as maps) has larger or the same amount of elements or has each corresponding element higher in ordering or the same as in the given argument. See information about ordering. |
Between, IsBetween | findByAddressBetween(Address lowerLimit, Address upperLimit) | …where x.age between ? and ?, …where x.firstName between ? and ? | Find records where address bin value (POJOs are stored in Aerospike as maps) is in the range between the given arguments. See information about ordering. |
And | findByAddressAndFriend(Address address, Person friend) | …where x.lastName = ? and x.firstName = ? | |
Or | findByAddressOrFriend(Address address, Person friend) | …where x.lastName = ? or x.firstName = ? |
Id repository queries
Keyword | Repository query sample | Snippet |
---|---|---|
No keyword | findById(String id) | …where x.PK = ? |
And | findByIdAndFirstName(String id, String firstName) | …where x.PK = ? and x.firstName = ? |
Query modification
Query modifiers
Keyword | Repository query sample | Snippet |
---|---|---|
IgnoreCase | findByLastNameIgnoreCase | …where UPPER(x.lastName) = UPPER(?) |
OrderBy | findByLastNameOrderByFirstNameDesc | …where x.lastName = ? order by x.firstName desc |
Limiting query results
Keyword | Repository query sample | Snippet |
---|---|---|
First | findFirstByAge | select top 1 where x.age = ? |
First N | findFirst3ByAge | select top 3 where x.age = ? |
Top | findTopByLastNameStartingWith | select top 1 where x.lastName like ‘abc%’ = ? |
Top N | findTop4ByLastNameStartingWith | select top 4 where x.lastName like ‘abc%‘ |
Distinct | findDistinctByFirstNameContaining | select distinct … where x.firstName like ‘abc%’ |
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
orfindTop20ByLastName
.
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);