Evolution of persistence Spring Data Alexandr Tretyakov Dnipropetrovs’k 31 July 2014
2 Agenda  The NoSQL landscape  Spring Data JPA  MongoDB in a nutshell  Spring Data MongoDB 4 August 2014
34 August 2014 The NoSQL landscape
4 Variety of databases 4 August 2014
54 August 2014 Spring Data JPA
6 Project goals 4 August 2014  Spring Data provides a familiar and consistent Spring-based programming model for NoSQL and relational stores while retaining store-specific features and capabilities.
7 Spring Data sub-projects 4 August 2014  Spring Data JPA  Spring Data MongoDB  Spring Data Neo4J  Spring Data Redis  Spring for Apache Hadoop  Spring Data Gemfire  Spring Data REST  Spring Data JDBC Extensions
8 Don't repeat the DAO 4 August 2014 public interface GenericDao <T, PK extends Serializable> { /* Persist the newInstance object into database */ PK create(T newInstance); /** Retrieve an object that was previously persisted to the database using the indicated id as primary key */ T read(PK id); /* Save changes made to a persistent object */ void update(T transientObject); /** Remove an object from persistent storage in the database */ void delete(T persistentObject); }
9 Don't repeat the DAO 4 August 2014 public class GenericDaoHibernateImpl <T, PK extends Serializable> implements GenericDao <T, PK> { private Class<T> type; public GenericDaoHibernateImpl(Class<T> type) { this.type = type; } public PK create(T o) { return (PK) getSession().save(o); } public T read(PK id) { return (T) getSession().get(type, id); } // … }
10 Don't repeat the DAO 4 August 2014 public interface PersonDao extends GenericDao<Person, Long> { List<Person> findByName(String name); } public class PersonDaoImpl implements PersonDao { //implementation }  The concept of a single generic typesafe DAO had been a topic since the appearance of generics in the Java language.
11 Spring Data managed DAO 4 August 2014 Repository interface abstraction has predefined set of methods for basic functionality.  Repository – A plain marker interface to let the Spring Data infrastructure pick up user- defined repositories  CrudRepository – Extends Repository and adds basic persistence methods like saving, finding, and deleting entities  PagingAndSortingRepository – Extends CrudRepository and adds methods for accessing entities page by page and sorting them by given criteria
12 Spring Data managed DAO 4 August 2014 @NoRepositoryBean public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity); T findOne(ID id); Iterable<T> findAll(); long count(); boolean exists(ID id); // … more functionality omitted }
13 Spring Data JPA features 4 August 2014  Sophisticated support to build repositories based on Spring and JPA  Support for Querydsl predicates and thus type-safe JPA queries  Pagination support, dynamic query execution, ability to integrate custom data access code  Validation of @Query annotated queries at bootstrap time  Support for XML based entity mapping  JavaConfig based repository configuration by introducing @EnableJpaRepositories.
14 Configuration (XML) 4 August 2014 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd”> <jpa:repositories base-package="com.itretiy.springdata.repository.jpa" repository-impl-postfix=”Impl” > <context:include-filter type=”regex” expression=”.*Repository” /> </jpa:repositories> </beans>
15 Configuration 4 August 2014  Create an interface either extending Repository or annotated with @RepositoryDefinition  Add the methods you want to provide (make sure that it complies with query derivation mechanism) Mostly that’s it what you need for start! (And of course do not forget to set up JPA EntityManager or SessionFactory if you are working directly with Hibernate).
16 Querying and fine-tuning the queries 4 August 2014  Query creation from method names – findBy*  Using JPA NamedQueries – @NamedQuery and @NamedNativeQuery  Using @Query – @Query(JPQL) – @Query(SQL) – @Query with @Param and @Modifying  Specifications for Criteria and Predicate  Querydsl predicates
17 Custom method implementations 4 August 2014  Add interface for custom repository functionality interface CustomRepository { public void customMethod(DomainClass domainClass); }  Add implementation of custom repository functionality class CustomRepositoryImpl implements CustomRepository { public void customMethod(DomainClass domainClass) { // Your custom implementation } }  Change your basic repository interface public interface BasicUserRepository extends CrudRepository<DomainClass, IdClass>, CustomRepository { // Declare other query methods here }
18 Fine-tuning repository definition 4 August 2014  Create an interface either extending Repository or annotated with @Repository Definition and annotate it with @NoRepositoryBean  Add the methods you want to expose to it and make sure they actually match the signatures of methods provided by the Spring Data base repository interfaces @NoRepositoryBean interface MyBaseRepository<T,ID extends Serializable> extends Repository<T, ID> { T findOne(ID id); T save(T entity); }  Use this interface as a base interface for the interface declarations for your entities interface UserRepository extends MyBaseRepository<User, Long> { User findByEmailAddress(EmailAddress emailAddress); }
19 QueryDSL integration 4 August 2014  Set up maven-apt-plugin with appropriate processor – QuerydslAnnotationProcessor – JPAAnnotationProcessor – HibernateAnnotationProcessor – JDOAnnotationProcessor – MongoAnnotationProcessor  Extend QueryDslPredicateExecutor in your repository interface
20 Spring Data JPA weaknesses 4 August 2014  Less of freedom, less flexibility and more conventions (arguably)  Оne more layer, hiding details (arguably)
214 August 2014 MongoDB in a nutshell
22 Terminology 4 August 2014 RDBMS MongoDB Database Database Table Collection Row(s) JSON Document Row(s) Field Index Index Join Embedded documents & linking
23 Document data model 4 August 2014  Document data model { _id: ObjectID('4bd9e8e17cefd644108961bb'), name: “The Godfather”, details: { isbn: “0451217403”, author: “Mario Puzo” }, price: 100.00, category: [ new ObjectID('4bf9bec50e32f82523389314')], comments: [ { user: “Don Corleone”, text: “Perfect!”, } ] }  Schemaless { _id: ObjectID('4bf9bec50e32f82523389315'), name: “Lvivske”, price: 5.50, category: [ new ObjectID('4bf9bec50e32f82523389316')] }
24 Key features 4 August 2014  Rich queries – If you’re coming from a relational database system where ad hoc queries are the norm, then it is sufficient to note that MongoDB features a similar level of queryability  Secondary indexes – Secondary indexes in MongoDB are implemented as B-trees. The kinds of indexes supported include all the ones you would find in an RDMBS; ascending, descending, unique, compound-key, and even geospatial  Lack of multi-document transactions  No RDBMS like join support – Two ways for relating documents: manual references and DBRefs
25 Replication 4 August 2014  Replica set
26 Scaling 4 August 2014  Auto-sharding
27 MongoDB Java Driver 4 August 2014 Mongo mongo = new MongoClient("localhost", 27017); mongo.setWriteConcern(WriteConcern.SAFE); DB database = mongo.getDB("database"); DBCollection collection = database.getCollection("collection"); DBObject data = new BasicDBObject(); data.put("key", "value"); collection.insert(data);
284 August 2014 Spring Data MongoDB
29 Spring Data MongoDB feautures 4 August 2014  MongoTemplate – take care of acquiring a connection through the configured MongoDbFactory and clean it up properly after the interaction with the store has ended or an exception occurred – Translate Mongo specific exception into DataAccessException hierarchy – Provide general-purpose, high-level methods that enable you to execute commonly needed operations as one-line statements (like findOne, findAll, etc.) – Provide low-level, callback-driven methods that allow you to interact with the MongoDB driver API (starts with execute*)  Object mapping – Annotation based: @Document, @Field, @Index  Repository support – Queries are derived from method signatures – Geospatial queries  Cross-store support (worth saying it is pretty poor)
30 Spring data mapping 4 August 2014  Abstraction MongoMappingContext – responsible for building up the domain class metamodel to avoid reflection lookups (e.g., to detect the id property or determine the field key on each and every persistence operation)  Abstraction MappingMongoConverter – performing the conversion using the mapping information provided by the MongoMappingContext  @Document annotation optional annotation that helps mapping subsystem convert domain objects to the Mongo specific DBObjects and vice versa  @Index for single properties and @CompoundIndex for multiple properties indexes  Do not need annotate relationship between parent and nested documents  @DBRef if you want denormalized model – support lazy loading – assume that related documents stored in the same database, because mongodb java driver does not support $db property in DBRefs – lack of support of cascading save  Converter interface abstraction for implementing custom converters
31 Configuration (XML) 4 August 2014 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <mongo:db-factory id="mongoDbFactory" dbname="test" /> <mongo:mapping-converter id="mongoConverter" base-package="com.itretiy.springdata.domain" db-factory-ref=”mongoDbFactory”/> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoDbFactory" /> <property name="writeConcern" value="SAFE" /> </bean> </beans>
32 Spring Data MongoDB repositories 4 August 2014  Spring Data MongoDB repositories and query derivation mechanism works as well as for JPA module (Obviously except for @Query syntax)
33 MongoDB JSON based query 4 August 2014  Placeholders ?* lets you substitute the value from the method arguments into the JSON query string public interface PersonRepository extends MongoRepository <Person, String> @Query("{ 'firstname' : ?0 }") List<Person> findByThePersonsFirstname(String firstname); }  Use the filter property to restrict the set of properties that will be mapped into the Java object public interface PersonRepository extends MongoRepository <Person, String> @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List<Person> findByThePersonsFirstname(String firstname); }
34 Spring Data MongoDB weaknesses 4 August 2014  Lack of cascade saving of related documents  Java driver do not support db in DBRef so related document must be saved in the same database (arguably)  Poor cross-store support
Your QR Code I am at your disposal in case of any questions or doubts 31 July 2014 Alexandr Tretyakov Dnipropetrovs’k itretiy@gmail.com

Александр Третьяков: "Spring Data JPA and MongoDB"

  • 1.
    Evolution of persistence SpringData Alexandr Tretyakov Dnipropetrovs’k 31 July 2014
  • 2.
    2 Agenda  The NoSQLlandscape  Spring Data JPA  MongoDB in a nutshell  Spring Data MongoDB 4 August 2014
  • 3.
    34 August 2014 TheNoSQL landscape
  • 4.
  • 5.
  • 6.
    6 Project goals 4 August2014  Spring Data provides a familiar and consistent Spring-based programming model for NoSQL and relational stores while retaining store-specific features and capabilities.
  • 7.
    7 Spring Data sub-projects 4August 2014  Spring Data JPA  Spring Data MongoDB  Spring Data Neo4J  Spring Data Redis  Spring for Apache Hadoop  Spring Data Gemfire  Spring Data REST  Spring Data JDBC Extensions
  • 8.
    8 Don't repeat theDAO 4 August 2014 public interface GenericDao <T, PK extends Serializable> { /* Persist the newInstance object into database */ PK create(T newInstance); /** Retrieve an object that was previously persisted to the database using the indicated id as primary key */ T read(PK id); /* Save changes made to a persistent object */ void update(T transientObject); /** Remove an object from persistent storage in the database */ void delete(T persistentObject); }
  • 9.
    9 Don't repeat theDAO 4 August 2014 public class GenericDaoHibernateImpl <T, PK extends Serializable> implements GenericDao <T, PK> { private Class<T> type; public GenericDaoHibernateImpl(Class<T> type) { this.type = type; } public PK create(T o) { return (PK) getSession().save(o); } public T read(PK id) { return (T) getSession().get(type, id); } // … }
  • 10.
    10 Don't repeat theDAO 4 August 2014 public interface PersonDao extends GenericDao<Person, Long> { List<Person> findByName(String name); } public class PersonDaoImpl implements PersonDao { //implementation }  The concept of a single generic typesafe DAO had been a topic since the appearance of generics in the Java language.
  • 11.
    11 Spring Data managedDAO 4 August 2014 Repository interface abstraction has predefined set of methods for basic functionality.  Repository – A plain marker interface to let the Spring Data infrastructure pick up user- defined repositories  CrudRepository – Extends Repository and adds basic persistence methods like saving, finding, and deleting entities  PagingAndSortingRepository – Extends CrudRepository and adds methods for accessing entities page by page and sorting them by given criteria
  • 12.
    12 Spring Data managedDAO 4 August 2014 @NoRepositoryBean public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { <S extends T> S save(S entity); T findOne(ID id); Iterable<T> findAll(); long count(); boolean exists(ID id); // … more functionality omitted }
  • 13.
    13 Spring Data JPAfeatures 4 August 2014  Sophisticated support to build repositories based on Spring and JPA  Support for Querydsl predicates and thus type-safe JPA queries  Pagination support, dynamic query execution, ability to integrate custom data access code  Validation of @Query annotated queries at bootstrap time  Support for XML based entity mapping  JavaConfig based repository configuration by introducing @EnableJpaRepositories.
  • 14.
    14 Configuration (XML) 4 August2014 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd”> <jpa:repositories base-package="com.itretiy.springdata.repository.jpa" repository-impl-postfix=”Impl” > <context:include-filter type=”regex” expression=”.*Repository” /> </jpa:repositories> </beans>
  • 15.
    15 Configuration 4 August 2014 Create an interface either extending Repository or annotated with @RepositoryDefinition  Add the methods you want to provide (make sure that it complies with query derivation mechanism) Mostly that’s it what you need for start! (And of course do not forget to set up JPA EntityManager or SessionFactory if you are working directly with Hibernate).
  • 16.
    16 Querying and fine-tuningthe queries 4 August 2014  Query creation from method names – findBy*  Using JPA NamedQueries – @NamedQuery and @NamedNativeQuery  Using @Query – @Query(JPQL) – @Query(SQL) – @Query with @Param and @Modifying  Specifications for Criteria and Predicate  Querydsl predicates
  • 17.
    17 Custom method implementations 4August 2014  Add interface for custom repository functionality interface CustomRepository { public void customMethod(DomainClass domainClass); }  Add implementation of custom repository functionality class CustomRepositoryImpl implements CustomRepository { public void customMethod(DomainClass domainClass) { // Your custom implementation } }  Change your basic repository interface public interface BasicUserRepository extends CrudRepository<DomainClass, IdClass>, CustomRepository { // Declare other query methods here }
  • 18.
    18 Fine-tuning repository definition 4August 2014  Create an interface either extending Repository or annotated with @Repository Definition and annotate it with @NoRepositoryBean  Add the methods you want to expose to it and make sure they actually match the signatures of methods provided by the Spring Data base repository interfaces @NoRepositoryBean interface MyBaseRepository<T,ID extends Serializable> extends Repository<T, ID> { T findOne(ID id); T save(T entity); }  Use this interface as a base interface for the interface declarations for your entities interface UserRepository extends MyBaseRepository<User, Long> { User findByEmailAddress(EmailAddress emailAddress); }
  • 19.
    19 QueryDSL integration 4 August2014  Set up maven-apt-plugin with appropriate processor – QuerydslAnnotationProcessor – JPAAnnotationProcessor – HibernateAnnotationProcessor – JDOAnnotationProcessor – MongoAnnotationProcessor  Extend QueryDslPredicateExecutor in your repository interface
  • 20.
    20 Spring Data JPAweaknesses 4 August 2014  Less of freedom, less flexibility and more conventions (arguably)  Оne more layer, hiding details (arguably)
  • 21.
  • 22.
    22 Terminology 4 August 2014 RDBMSMongoDB Database Database Table Collection Row(s) JSON Document Row(s) Field Index Index Join Embedded documents & linking
  • 23.
    23 Document data model 4August 2014  Document data model { _id: ObjectID('4bd9e8e17cefd644108961bb'), name: “The Godfather”, details: { isbn: “0451217403”, author: “Mario Puzo” }, price: 100.00, category: [ new ObjectID('4bf9bec50e32f82523389314')], comments: [ { user: “Don Corleone”, text: “Perfect!”, } ] }  Schemaless { _id: ObjectID('4bf9bec50e32f82523389315'), name: “Lvivske”, price: 5.50, category: [ new ObjectID('4bf9bec50e32f82523389316')] }
  • 24.
    24 Key features 4 August2014  Rich queries – If you’re coming from a relational database system where ad hoc queries are the norm, then it is sufficient to note that MongoDB features a similar level of queryability  Secondary indexes – Secondary indexes in MongoDB are implemented as B-trees. The kinds of indexes supported include all the ones you would find in an RDMBS; ascending, descending, unique, compound-key, and even geospatial  Lack of multi-document transactions  No RDBMS like join support – Two ways for relating documents: manual references and DBRefs
  • 25.
  • 26.
  • 27.
    27 MongoDB Java Driver 4August 2014 Mongo mongo = new MongoClient("localhost", 27017); mongo.setWriteConcern(WriteConcern.SAFE); DB database = mongo.getDB("database"); DBCollection collection = database.getCollection("collection"); DBObject data = new BasicDBObject(); data.put("key", "value"); collection.insert(data);
  • 28.
  • 29.
    29 Spring Data MongoDBfeautures 4 August 2014  MongoTemplate – take care of acquiring a connection through the configured MongoDbFactory and clean it up properly after the interaction with the store has ended or an exception occurred – Translate Mongo specific exception into DataAccessException hierarchy – Provide general-purpose, high-level methods that enable you to execute commonly needed operations as one-line statements (like findOne, findAll, etc.) – Provide low-level, callback-driven methods that allow you to interact with the MongoDB driver API (starts with execute*)  Object mapping – Annotation based: @Document, @Field, @Index  Repository support – Queries are derived from method signatures – Geospatial queries  Cross-store support (worth saying it is pretty poor)
  • 30.
    30 Spring data mapping 4August 2014  Abstraction MongoMappingContext – responsible for building up the domain class metamodel to avoid reflection lookups (e.g., to detect the id property or determine the field key on each and every persistence operation)  Abstraction MappingMongoConverter – performing the conversion using the mapping information provided by the MongoMappingContext  @Document annotation optional annotation that helps mapping subsystem convert domain objects to the Mongo specific DBObjects and vice versa  @Index for single properties and @CompoundIndex for multiple properties indexes  Do not need annotate relationship between parent and nested documents  @DBRef if you want denormalized model – support lazy loading – assume that related documents stored in the same database, because mongodb java driver does not support $db property in DBRefs – lack of support of cascading save  Converter interface abstraction for implementing custom converters
  • 31.
    31 Configuration (XML) 4 August2014 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <mongo:db-factory id="mongoDbFactory" dbname="test" /> <mongo:mapping-converter id="mongoConverter" base-package="com.itretiy.springdata.domain" db-factory-ref=”mongoDbFactory”/> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoDbFactory" /> <property name="writeConcern" value="SAFE" /> </bean> </beans>
  • 32.
    32 Spring Data MongoDBrepositories 4 August 2014  Spring Data MongoDB repositories and query derivation mechanism works as well as for JPA module (Obviously except for @Query syntax)
  • 33.
    33 MongoDB JSON basedquery 4 August 2014  Placeholders ?* lets you substitute the value from the method arguments into the JSON query string public interface PersonRepository extends MongoRepository <Person, String> @Query("{ 'firstname' : ?0 }") List<Person> findByThePersonsFirstname(String firstname); }  Use the filter property to restrict the set of properties that will be mapped into the Java object public interface PersonRepository extends MongoRepository <Person, String> @Query(value="{ 'firstname' : ?0 }", fields="{ 'firstname' : 1, 'lastname' : 1}") List<Person> findByThePersonsFirstname(String firstname); }
  • 34.
    34 Spring Data MongoDBweaknesses 4 August 2014  Lack of cascade saving of related documents  Java driver do not support db in DBRef so related document must be saved in the same database (arguably)  Poor cross-store support
  • 35.
    Your QR Code I amat your disposal in case of any questions or doubts 31 July 2014 Alexandr Tretyakov Dnipropetrovs’k itretiy@gmail.com