Get ahead
VMware offers training and certification to turbo-charge your progress.
Learn moreI'd like to think I'm pretty comfortable with Java and generics but I recently came across this bit of Java code and it stopped me in my tracks :
public abstract class AnnotationBasedPersistentProperty<P extends PersistentProperty<P>> extends AbstractPersistentProperty<P> {..}
This class is internal to the Spring Data framework's Repository Support which removes the need to write boilerplate code when implementing a data access layer and also provides a common programming model for mapping domain objects and managing data access to any type of persistent store. Spring Data's current repository implementations include relational databases (JPA), Gemfire,MongoDB, Neo4.
Fortunately, if you use Spring Data in your Java Application, you won't ever see this code. It's hidden behind Spring Data's simple yet powerful Repository abstraction providing queries, paging, sorting, and common CRUD operations. For example:
public class SomeClient {
@Autowired
private PersonRepository repository;
public void doSomething() {
List<Person> persons = repository.findByLastname("Matthews");
}
}
As stated in the Spring Data project mission statement:
Spring Data provides a familiar and consistent Spring-based programming model for NoSQL and relational stores while retaining store-specific features and capabilities.
A key feature of this familiar and consistent programming model, when it comes to repositories, is the use of annotations to configure persistent object mapping. This idea was first proven with Hibernate and subsequently adapted to JPA. Retaining store-specific features requires that the core framework provide some generic annotations, such as @Id and @Transient, and also support for store-specific annotations.
To support mapping a domain object's properties, a Spring Data Repository implementation should subclass AnnotationBasedPersistentProperty. This class provides common annotation handling logic across persistent and provides hooks for custom annotation handling that is unique for a given store. This class happens to extend another abstract class called AbstractPersistentProperty which is declared as follows:
public abstract class AbstractPersistentProperty<P extends PersistentProperty<P>> implements PersistentProperty<P>
public interface PersistentProperty<P extends PersistentProperty<P>>
PersistentEntity<?, P> getOwner();
PersistentEntity is an abstract representation of the class of a persisted domain object. It is designed to address class level mapping concerns and contains references to - wait for it - Persistent properties. PersistentEntity is strongly typed to the extended PersistentProperty interface, and also exposes the domain class type which is represented here by the wildcard ? formal parameter.
So let's take another look at the initial type declaration:
public abstract class AnnotationBasedPersistentProperty<P extends PersistentProperty<P>> extends AbstractPersistentProperty<P> {..}
public interface MongoPersistentProperty extends PersistentProperty<MongoPersistentProperty>{...}
public interface GemfirePersistentProperty extends PersistentProperty<GemfirePersistentProperty>{...}