Spring Data Oleksii Usyk
Contents • Spring Data overview • Core functionality – Repository support – Templating – Object/Datastore mapping • Spring data extensions: – Web support – Repository populations • Practice
Spring Data • Spring Data is high level SpringSource project • Purpose: – Ease the work with persistence stores – Support for new data access technologies – Improve support for RDBMS – Unify the access to different kinds of persistence stores, both RDBMS and NoSQL* data stores.
Spring Data project consists of:
Structure
Programming model Client Repository Template Object Mapping Datastore
Templating The main purpose of all Spring templates is resource allocation and exception translation. Spring Data: Resourse - datastore ExceptionTranslation – from datastore driver specific exceptions to well known unchecked DataAccessException hierarchy
Implementations of Templates Store specific implementations: • MongoTemplate • EntityManager – acts like template for JPA • GemfireTemplate • Neo4jTemplate • RedisTemplate • …
Object/Datastore mapping {store}MappingConverter with default configuration is created implicitly: Mapping is based on: – Convention – Metadata Explicit conversion {store}MappingConverter can be configured explicitly via both XML and Java
Object/Datastore mapping GemFire References to other objects
Repository The goal of Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. org.springframework.data.repository.Repository org.springframework.data.repository.CrudRepository org.springframework.data.repository.PagingAndSortingRepository
Cooking Spring Data 1) Declare an interface extending Repository or one of its subinterfaces and type it to the domain class: 2) Declare query methods on the interface: 3) Set up Spring to create proxy instances for those interfaces: 4) Get the repository instance injected and use it:
How it works? • Spring Data provides persistence store specific implementations for all interfaces that extend Repository. • … no need to write an implementation? simple cases – yes more complex operations – require some additional code Benefits: • no boilerplate for simple CRUD • full control on persisting
Query creation
Defining query methods • The repository proxy has two ways to derive a store-specific query from the method name: – Method name – Additionally created query • Strategies: • CREATE • USE_DECLARED_QUERY • CREATE_IF_NOT_FOUND (default)
Custom implementations 1. Define an interface 2. Repository interface should extend the custom interface: 3. Implement the defined interface with the custom functionality 4. Put custom interface implementation in the appropriate place:
Configuration
Spring Data Web support • Integration with Spring MVC • No need to write boilerplate code in controllers • Spring Data REST project provides a solid foundation on which to expose CRUD operations to your JPA Repository-managed entities using plain HTTP REST semantics.
Web Support configuration Following components will be registered in basic (not HATEOAS) case: • DomainClassConverter: enables Spring MVC to resolve instances of repository managed domain classes from request parameters or path variables. • HandlerMethodArgumentResolver implementations to let Spring MVC resolve Pageable and Sort instances from request parameters.
Web Support examples • DomainClassConverter usage: • PageableHandlerMethodArgumentResolver and SortHandlerMethodArgumentResolver usage: Request Parameter Description page Page you want to retrieve. size Size of the page you want to retrieve. sort Properties that should be sorted by in the format property,property(,ASC| DESC).
Repository populators • Can specify data to populate storage independent format: JSON and XML • JSON populator configuration: • XML populator configuration:
JPA support Provides enhanced support for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.
MongoDB support MongoDB (from "humongous") is an opensource document database, and the leading NoSQL database written in C++. Spring Data MongoDB works with • MongoDB 1.4 or higher • Java SE 5 or higher.
GemFire support vFabric GemFire is a distributed data management platform providing dynamic scalability, high performance, and database-like persistence. • JDK > 6.0 • Spring Framework 3 • vFabric GemFire > 6.6
Practice
Questions
Useful links Spring Data • • • • • • • http://projects.spring.io/spring-data/ http://spring.io/guides/tutorials/data/ http://docs.spring.io/spring-data/ http://www.infoq.com/articles/spring-data-intro http://www.slideshare.net/mcgray/spring-data-new-approach-to-persistence http://www.odbms.org/blog/2013/01/the-spring-data-project-interview-with-david-turanski/ http://spring.io/guides/gs/accessing-data-mongo/ JPA http://www.spring-source.ru/docs_simple.php?type=manual&theme=docs_simple&docs_simple=chap03_p02 Mongo http://docs.mongodb.org/manual/ GemFire http://community.gemstone.com/display/gemfire/GemFire+Tutorial
That’s all folks Thank you for your attention!
Spring Data MongoDB features • Spring configuration support using Java based @Configuration classes or an XML namespace for a Mongo driver instance and replica sets • MongoTemplate helper class that increases productivity performing common Mongo operations. Includes integrated object mapping between documents and POJOs. • Exception translation into Spring's portable Data Access Exception hierarchy • Feature Rich Object Mapping integrated with Spring's Conversion Service • Annotation based mapping metadata but extensible to support other metadata formats • Persistence and mapping lifecycle events • Java based Query, Criteria, and Update DSLs • Automatic implementation of Repository interfaces including support for custom finder methods. • QueryDSL integration to support type-safe queries. • Cross-store persistence - support for JPA Entities with fields transparently persisted/retrieved using MongoDB • Log4j log appender • GeoSpatial integration
Mongo Configuration
MongoTemplate • The class MongoTemplate is the central class of the Spring's MongoDB support providing a rich feature set to interact with the database. • Provides wide range of convenience CRUD operations for MongoDB documents • Provides a mapping between domain objects and MongoDB documents. • Provides similar functionality to MongoDB driver methods but also gracefully handles mapping for you. (Using pure driver methods you have to deal with DBObject instead of your domain object.)
MongoDB type mapping Java Person class Document in mongo “prs” collection
Mapping annotations overview • • • • • • • • • • • • • • • • • • • • • The MappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below @Id - applied at the field level to mark the field used for identiy purpose. @Document - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored. @DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef. @Indexed - applied at the field level to describe how to index the field. @CompoundIndex - applied at the type level to declare Compound Indexes @GeoSpatialIndexed - applied at the field level to describe how to geoindex the field. @Transient - by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database @PersistenceConstructor - marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject. @Value - this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object. In order to reference a property of a given document one has to use expressions like: @Value("#root.myProperty") where root refers to the root of the given document. @Field - applied at the field level and described the name of the field as it will be represented in the MongoDB BSON document thus allowing the name to be different than the fieldname of the class.
Custom Converters
MongoTemplate features • MapReduce operations support • Group operations support • Aggregation Framework support (introduced in Mongo version 2.2) • Index and Collection management • Executing Commands • Lifecycle Events (do smth Before,After x Save,Load,Convert) • Exception Translation (DataAccessException and its children) • GridFs support
Repository
MongoDB JSON based query methods and field restriction @org.springframework.data.mongodb.repository.Query Simple querying: Querying with field restriction:
QueryDSL and type-safe query methods MongoDB repository support integrates with the QueryDSL project which provides a means to perform type-safe queries in Java. Features: • Code completion in IDE (all properties, methods and operations can be expanded in your favorite Java IDE) • Almost no syntactically invalid queries allowed (type-safe on all levels) • Domain types and properties can be referenced safely (no Strings involved!) • Adopts better to refactoring changes in domain types • Incremental query definition is easier
QueryDSL and type-safe query methods 1) QueryDslPredicateExecutor interface is provided for you by Spring Data 2) Just make your repository interface extend QueryDslPredicateExecutor 3) Enjoy type-safe queries
Miscellaneous • Cross store support • Logging support • JMX support

Spring data presentation

  • 1.
  • 2.
    Contents • Spring Dataoverview • Core functionality – Repository support – Templating – Object/Datastore mapping • Spring data extensions: – Web support – Repository populations • Practice
  • 3.
    Spring Data • SpringData is high level SpringSource project • Purpose: – Ease the work with persistence stores – Support for new data access technologies – Improve support for RDBMS – Unify the access to different kinds of persistence stores, both RDBMS and NoSQL* data stores.
  • 4.
    Spring Data projectconsists of:
  • 5.
  • 6.
  • 7.
    Templating The main purposeof all Spring templates is resource allocation and exception translation. Spring Data: Resourse - datastore ExceptionTranslation – from datastore driver specific exceptions to well known unchecked DataAccessException hierarchy
  • 8.
    Implementations of Templates Storespecific implementations: • MongoTemplate • EntityManager – acts like template for JPA • GemfireTemplate • Neo4jTemplate • RedisTemplate • …
  • 9.
    Object/Datastore mapping {store}MappingConverter withdefault configuration is created implicitly: Mapping is based on: – Convention – Metadata Explicit conversion {store}MappingConverter can be configured explicitly via both XML and Java
  • 10.
  • 11.
    Repository The goal ofSpring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores. org.springframework.data.repository.Repository org.springframework.data.repository.CrudRepository org.springframework.data.repository.PagingAndSortingRepository
  • 12.
    Cooking Spring Data 1)Declare an interface extending Repository or one of its subinterfaces and type it to the domain class: 2) Declare query methods on the interface: 3) Set up Spring to create proxy instances for those interfaces: 4) Get the repository instance injected and use it:
  • 13.
    How it works? •Spring Data provides persistence store specific implementations for all interfaces that extend Repository. • … no need to write an implementation? simple cases – yes more complex operations – require some additional code Benefits: • no boilerplate for simple CRUD • full control on persisting
  • 14.
  • 15.
    Defining query methods •The repository proxy has two ways to derive a store-specific query from the method name: – Method name – Additionally created query • Strategies: • CREATE • USE_DECLARED_QUERY • CREATE_IF_NOT_FOUND (default)
  • 16.
    Custom implementations 1. Definean interface 2. Repository interface should extend the custom interface: 3. Implement the defined interface with the custom functionality 4. Put custom interface implementation in the appropriate place:
  • 17.
  • 18.
    Spring Data Websupport • Integration with Spring MVC • No need to write boilerplate code in controllers • Spring Data REST project provides a solid foundation on which to expose CRUD operations to your JPA Repository-managed entities using plain HTTP REST semantics.
  • 19.
    Web Support configuration Followingcomponents will be registered in basic (not HATEOAS) case: • DomainClassConverter: enables Spring MVC to resolve instances of repository managed domain classes from request parameters or path variables. • HandlerMethodArgumentResolver implementations to let Spring MVC resolve Pageable and Sort instances from request parameters.
  • 20.
    Web Support examples •DomainClassConverter usage: • PageableHandlerMethodArgumentResolver and SortHandlerMethodArgumentResolver usage: Request Parameter Description page Page you want to retrieve. size Size of the page you want to retrieve. sort Properties that should be sorted by in the format property,property(,ASC| DESC).
  • 21.
    Repository populators • Canspecify data to populate storage independent format: JSON and XML • JSON populator configuration: • XML populator configuration:
  • 22.
    JPA support Provides enhancedsupport for JPA based data access layers. It makes it easier to build Spring-powered applications that use data access technologies.
  • 23.
    MongoDB support MongoDB (from"humongous") is an opensource document database, and the leading NoSQL database written in C++. Spring Data MongoDB works with • MongoDB 1.4 or higher • Java SE 5 or higher.
  • 24.
    GemFire support vFabric GemFireis a distributed data management platform providing dynamic scalability, high performance, and database-like persistence. • JDK > 6.0 • Spring Framework 3 • vFabric GemFire > 6.6
  • 25.
  • 26.
  • 27.
  • 28.
    That’s all folks Thankyou for your attention!
  • 34.
    Spring Data MongoDBfeatures • Spring configuration support using Java based @Configuration classes or an XML namespace for a Mongo driver instance and replica sets • MongoTemplate helper class that increases productivity performing common Mongo operations. Includes integrated object mapping between documents and POJOs. • Exception translation into Spring's portable Data Access Exception hierarchy • Feature Rich Object Mapping integrated with Spring's Conversion Service • Annotation based mapping metadata but extensible to support other metadata formats • Persistence and mapping lifecycle events • Java based Query, Criteria, and Update DSLs • Automatic implementation of Repository interfaces including support for custom finder methods. • QueryDSL integration to support type-safe queries. • Cross-store persistence - support for JPA Entities with fields transparently persisted/retrieved using MongoDB • Log4j log appender • GeoSpatial integration
  • 35.
  • 36.
    MongoTemplate • The classMongoTemplate is the central class of the Spring's MongoDB support providing a rich feature set to interact with the database. • Provides wide range of convenience CRUD operations for MongoDB documents • Provides a mapping between domain objects and MongoDB documents. • Provides similar functionality to MongoDB driver methods but also gracefully handles mapping for you. (Using pure driver methods you have to deal with DBObject instead of your domain object.)
  • 37.
    MongoDB type mapping JavaPerson class Document in mongo “prs” collection
  • 38.
    Mapping annotations overview • • • • • • • • • • • • • • • • • • • • • TheMappingMongoConverter can use metadata to drive the mapping of objects to documents. An overview of the annotations is provided below @Id - applied at the field level to mark the field used for identiy purpose. @Document - applied at the class level to indicate this class is a candidate for mapping to the database. You can specify the name of the collection where the database will be stored. @DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef. @Indexed - applied at the field level to describe how to index the field. @CompoundIndex - applied at the type level to declare Compound Indexes @GeoSpatialIndexed - applied at the field level to describe how to geoindex the field. @Transient - by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database @PersistenceConstructor - marks a given constructor - even a package protected one - to use when instantiating the object from the database. Constructor arguments are mapped by name to the key values in the retrieved DBObject. @Value - this annotation is part of the Spring Framework . Within the mapping framework it can be applied to constructor arguments. This lets you use a Spring Expression Language statement to transform a key's value retrieved in the database before it is used to construct a domain object. In order to reference a property of a given document one has to use expressions like: @Value("#root.myProperty") where root refers to the root of the given document. @Field - applied at the field level and described the name of the field as it will be represented in the MongoDB BSON document thus allowing the name to be different than the fieldname of the class.
  • 39.
  • 40.
    MongoTemplate features • MapReduceoperations support • Group operations support • Aggregation Framework support (introduced in Mongo version 2.2) • Index and Collection management • Executing Commands • Lifecycle Events (do smth Before,After x Save,Load,Convert) • Exception Translation (DataAccessException and its children) • GridFs support
  • 41.
  • 42.
    MongoDB JSON basedquery methods and field restriction @org.springframework.data.mongodb.repository.Query Simple querying: Querying with field restriction:
  • 43.
    QueryDSL and type-safequery methods MongoDB repository support integrates with the QueryDSL project which provides a means to perform type-safe queries in Java. Features: • Code completion in IDE (all properties, methods and operations can be expanded in your favorite Java IDE) • Almost no syntactically invalid queries allowed (type-safe on all levels) • Domain types and properties can be referenced safely (no Strings involved!) • Adopts better to refactoring changes in domain types • Incremental query definition is easier
  • 44.
    QueryDSL and type-safequery methods 1) QueryDslPredicateExecutor interface is provided for you by Spring Data 2) Just make your repository interface extend QueryDslPredicateExecutor 3) Enjoy type-safe queries
  • 45.
    Miscellaneous • Cross storesupport • Logging support • JMX support

Editor's Notes

  • #4 Проект верхнего уровня SpringSource Направлен на упрощение работы с хранилищами данных Поддержка новейших технологий хранения данных, таких как NoSQL, Map-reduce frameworks, cloud-based data services. Целью является создание унифицированного доступа к различным типам хранилищ данных: реляционных, NoSQL, etc * NoSQL – в данном контексте подразумевается весь спектр не реляционных технологии хранения данных
  • #5 Основные проекты: Spring data for JPA, MongoDB, Neo4j, Redis, Hadoop, Gemfire, REST, Extensions Комьюнити проекты: Solr, Couchbase, Elasticseacrh JPA –Java Persistence API – management of relation data. MongoDB – is a cross-platform document-oriented database system. (stores JSON documents, dynamic schema) Gemfire - distributed data management platform providing dynamic scalability, high performance, and database-like persistence Neo4j -  open-source graph database, implemented in Java. Embedded, disk-based, fully transactional Java persistence engine that stores data structured in graphs rather than in tables. Высокопроизводительная, NoSQL база данных основанная на принципе графов. В ней нет такого понятия как таблицы со строго заданными полями, она оперирует гибкой структурой в виде нод и связей между ними. REDIS -  networked, in-memory, key-value data store with optional durability. Hadoop –  storage and large scale processing of data-sets on clusters Couchbase - open source, distributed (shared-nothing architecture) NoSQL document-oriented database that is optimized for interactive applications. Elasticsearch is a distributed, RESTful, free/open source search server based on Lucene.  Solr -  is an open source enterprise search platform from the Apache Lucene project. Its major features include full-text search, hit highlighting, faceted search, dynamic clustering, database integration, and rich document (e.g., Word, PDF) handling
  • #6 Все вместе это выглядит примерно таким образом. Мы видим что СД предоставляет нам унифицированный интерфейс для работы с большинством современных технологий хранения данных. В тоже время для каждого специфического хранилища мы из коробки получаем конкретную реализацию.
  • #7 Модель работы с данными Spring Data состоит из трех основных частей. Абстракция репозиторий – общий интерфейс для доступа к данным не зависимо от типа хранилища данных. Темплейт – инкапсулирует в себе само взаимодействие с хранилищем данных (коннекшн, сессии) – удобная обертка для драйвера. Отображение обьекта (маппинг) из объектной модели Java в модель которую поддерживает хранилище
  • #8 Темплейт предоставляет специфические для каждой конкретной технологии операции сохранения, модификации, удаления записи, а также возможность выполнять сложные запросы и мап/редьюс задачи. A template offers store specific operations like saving, updating and deleting a single record or for executing queries or map/reduce jobs. But all these methods work only for the corresponding underlying datastore. Spring Data JPA не предоставляет темплейта так как JPA имплементация сама по себе является абстрактным слоем над JDBC API. JPA’s EntityManager выступает в качетсве теплейта в случае JPA. Spring Data JPA does not offer a template, since the JPA implementation itself is already an abstraction layer on top of the JDBC API. JPA’s EntityManager is the counterpart of a template. Exception translation is handled by the repository implementation.
  • #11 Spring Data JPA reuses existing JPA annotation for O/R mapping, mongo and others should be annotated with spring data specific ones.
  • #12 Центральным понятием Spring Data framework является абстракция репозиторий. Абстракция репозиторий – общий интерфейс для доступа к данным не зависимо от типа хранилища данных. Основной задачей абстракции Репозиторий является значительной уменьшение количества кода который необходим для реализации слоя доступа к данным для разных хранилищ данных.
  • #13 @RepositoryDefinition – можно не наследоваться от @Repository, а просто пометить интерфейс как @RepositoryDefinition
  • #15 Полный список поддерживаемых ключевых слов можно найти в официальной спецификации. Также там можно подробнее ознакомится с алгоритмом по которому парсятся названия методов. AddressZipCode -> AddressZip.Code -> Address.ZipCode FindByAddressZipCode(ZipCode zc) = FindByAddress_ZipCode(ZipCode zc)
  • #16 Каким образом будет сформирован запрос специфичный для каждого типа хранилища данных? 2 варианта: Используя имя метода в репозитории Используя нативный запрос (аннотация)
  • #17 It is also possible to add custom behavior to all repositories
  • #18 Стоит отметить что для корректной работы Spring Data необходимо сконфигурировать соответствующий Template. (В случае JPA – entity manager)
  • #20 Integration with Spring HATEOAS
  • #21 The @EnableSpringDataWebSupport annotation registers a few components we will discuss in a bit. It will also detect Spring HATEOAS on the classpath and register integration components for it as well if present. The configuration snippet above also registers a PageableHandlerMethodArgumentResolver as well as an instance of SortHandlerMethodArgumentResolver. The registration enables Pageable and Sort being valid controller method arguments
  • #28 mongod –dbpath=D/mongo/tutorial ./gradlew run
  • #37 MongoTemplate(SimpleMongoDbFactory(MongoFactoryBean(host,port,MongoOptions()))) Can be configured with com.mongodb.Mongo object as well: MongoTemplate(Mongo())
  • #38 Once configured, MongoTemplate is thread-safe and can be reused across multiple instances.
  • #39 The following outlines what property will be mapped to the '_id' document field: • A property or field annotated with @Id (org.springframework.data.annotation.Id) will be mapped to the '_id' field. • A property or field without an annotation but named id will be mapped to the '_id' field. Can be BigInteger or String If no field or property specified above is present in the Java class then an implicit '_id' file will be generated by the driver but not mapped to a property or field of the Java class. To achieve that the MappingMongoConverter uses a MongoTypeMapper abstraction with DefaultMongoTypeMapper as it's main implementation. It's default behaviour is storing the fully qualified classname under _class inside the document for the top-level document as well as for every value if it's a complex type and a subtype of the property type declared. Type mapping can be customized.
  • #40 The mapping metadata infrastructure is defined in a seperate spring-data-commons project that is technology agnostic. Specific subclasses are using in the MongoDB support to support annotation based metadata. Other strategies are also possible to put in place if there is demand.
  • #41 Можно зарегистрировать собственные конвертеры для более полного контроля за процессом сохранения, доставания данных в/из монго. @ReadingConverter @WritingConverter for disambiguation.
  • #42 As an alternative to using Map-Reduce to perform data aggregation, you can use the group operation which feels similar to using SQL's group by query style, so it may feel more approachable vs. using Map-Reduce. Using the group operations does have some limitations, for example it is not supported in a shareded environment and it returns the full result set in a single BSON object, so the result should be small, less than 10,000 keys. Spring Data MongoDB provides support for the Aggregation Framework introduced to MongoDB in version 2.2. The MongoDB Documentation describes the Aggregation Framework as follows:“The MongoDB aggregation framework provides a means to calculate aggregated values without having to use mapreduce. While map-reduce is powerful, it is often more difficult than necessary for many simple aggregation tasks, such as totaling or averaging field values.” For further information see the full reference documentation of the aggregation framework and other data aggregation tools for MongoDB.
  • #46 QPerson is a class that is generated (via the Java annotation post processing tool) which is a Predicate that allows you to write type safe queries. Notice that there are no strings in the query other than the value "C0123".
  • #48 HATEOAS, an abbreviation for Hypermedia as the Engine of Application State, is a constraint of the REST application architecture that distinguishes it from most other network application architectures. The principle is that a client interacts with a network application entirely throughhypermedia provided dynamically by application servers. A REST client needs no prior knowledge about how to interact with any particular application or server beyond a generic understanding of hypermedia. In a service-oriented architecture (SOA), clients and servers interact through a fixedinterface shared through documentation or an interface description language (IDL). RESTful service can be described as well to be available for the client code-generation, RSDL (RESTful Service Description Language) using dynamic metadata collection to achieve this goal. The HATEOAS constraint serves to decouple client and server in a way that allows the server to evolve functionality independently. Spring HATEOAS ships with a representation model class PagedResources that allows enrichting the content of a Page instance with the necessary Page metadata as well as links to let the clients easily navigate the pages. The conversion of a Page to a PagedResources is done by an implementation of the Spring HATEOAS ResourceAssembler interface, the PagedResourcesAssembler.