9. GemFire Repositories

9.1 Introduction

Spring Data GemFire provides support to use the Spring Data repository abstraction to easily persist entities into GemFire and execute queries. A general introduction into the repository programming model is been provided here .

9.2 Spring configuration

To bootstrap Spring Data repositories you use the <repositories /> element from the GemFire namespace:

Example 9.1. Bootstrap GemFire repositories

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:gfe-data="http://www.springframework.org/schema/data/gemfire"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/data/gemfire 
                           http://www.springframework.org/schema/data/gemfire/spring-data-gemfire.xsd>

  <gfe-data:repositories base-package="com.acme.repository" />

</beans>

This configuration snippet will look for interfaces below the configured base package and create repository instances for those interfaces backed by a SimpleGemFireRepository. Note that you have to have your domain classes correctly mapped to configured regions as the bottstrap process will fail otherwise.

9.3 Executing OQL queries

The GemFire repositories allow the definition of query methods to easily execute OQL queries against the Region the managed entity is mapped to.

Example 9.2. Sample repository

@Region("myRegion")
public class Person { … }
public interface PersonRepository extends CrudRepository<Person, Long> {

  Person findByEmailAddress(String emailAddress);

  Collection<Person> findByFirstname(String firstname);

  @Query("SELECT * FROM /Person p WHERE p.firstname = $1")
  Collection<Person> findByFirstnameAnnotated(String firstname);

  @Query("SELECT * FROM /Person p WHERE p.firstname IN SET $1")
  Collection<Person> findByFirstnamesAnnotated(Collection<String> firstnames);
}

The first method listed here will cause the following query to be derived: SELECT x FROM /myRegion x WHERE x.emailAddress = $1. The second method works the same way except it's returning all entities found whereas the first one expects a single result value. In case the supported keywords are not sufficient to declare your query or the method name gets to verbose you can annotate the query methods with @Query as seen for methods 3 and 4.

Table 9.1. Supported keywords for query methods

KeywordSampleLogical result
GreaterThanfindByAgeGreaterThan(int age)x.age > $1
GreaterThanEqualfindByAgeGreaterThanEqual(int age)x.age >= $1
LessThanfindByAgeLessThan(int age)x.age < $1
LessThanEqualfindByAgeLessThanEqual(int age)x.age <= $1
IsNotNull, NotNullfindByFirstnameNotNull()x.firstname =! NULL
IsNull, NullfindByFirstnameNull()x.firstname = NULL
InfindByFirstnameIn(Collection<String> x)x.firstname IN SET $1
NotInfindByFirstnameNotIn(Collection<String> x)x.firstname NOT IN SET $1
(No keyword)findByFirstname(String name)x.firstname = $1
LikefindByFirstnameLike(String name)x.firstname LIKE $1
NotfindByFirstnameNot(String name)x.firstname != $1
IsTrue, TruefindByActiveIsTrue()x.active = true
IsFalse, FalsefindByActiveIsFalse()x.active = false