8. POJO mapping

8.1 Entity mapping

Spring Data GemFire provides support to map entities to be stored in a GemFire grid. The mapping metadata is define by using annotations at the domain classes just like this:

Example 8.1. Mapping a domain class to GemFire

@Region("myRegion")
public class Person {

  @Id Long id;
  String firstname;
  String lastname;

  @PersistenceConstructor
  public Person(String firstname, String lastname) {
    // …
  }

  …
}  

The first thing you see here is the @Region annotation that can be used to customize the region instances of the Person class are stored in. The @Id annotation can be used to annotate the property that shall be used as cache key. The @PersistenceConstructor annotation actually helps disambiguating multiple potentially available constructors taking parameters and explicitly marking the one annotated as the one to be used to create entities. With none or only a single constructor you can omit the annotation.

8.2 Mapping PDX serializer

Spring Data GemFire provides a custom PDXSerializer implementation that uses the mapping information to customize entity serialization. Beyond that it allows customizing the entity instantiation by using the Spring Data EntityInstantiator abstraction. By default the serializer uses a ReflectionEntityInstantiator that will use the persistence constructor of the mapped entity (either the single declared one or explicitly annoted with @PersistenceConstructor). To provide values for constructor parameters it will read fields with name of the constructor parameters from the PDXReader supplied.

Example 8.2. Using @Value on entity constructor parameters

public class Person {

  public Person(@Value("#root.foo") String firstname, @Value("bean") String lastname) {
    // …
  }

}  

The entity annotated as such will get the field foo read from the PDXReader and handed as constructor parameter value for firstname. The value for lastname will be the Spring bean with name bean.