Spring data presentation

45
Spring Data Oleksii Usyk

description

Presentation about main features about framework.

Transcript of Spring data presentation

Page 1: Spring data presentation

Spring Data

Oleksii Usyk

Page 2: Spring data presentation

Contents

• Spring Data overview• Core functionality

– Repository support– Templating– Object/Datastore mapping• Spring data extensions:– Web support– Repository populations• Practice

Page 3: Spring data presentation

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.

Page 4: Spring data presentation

Spring Data project consists of:

Page 5: Spring data presentation

Structure

Page 6: Spring data presentation

Programming model

Repository

Template

Object Mapping

Datastore

Client

Page 7: Spring data presentation

Templating

The main purpose of all Spring templates is resource allocation and exception translation.

Spring Data:Resourse - datastoreExceptionTranslation – from datastore driver

specific exceptions to well known unchecked DataAccessException hierarchy

Page 8: Spring data presentation

Implementations of Templates

Store specific implementations:• MongoTemplate• EntityManager – acts like template for JPA• GemfireTemplate• Neo4jTemplate• RedisTemplate• …

Page 9: Spring data presentation

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

Page 10: Spring data presentation

Object/Datastore mappingGemFire

References to other objects

Page 11: Spring data presentation

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

Page 12: Spring data presentation

Cooking Spring Data1) 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:

Page 13: Spring data presentation

How it works?• Spring Data provides persistence store specific

implementations for all interfaces that extend Repository.

• … no need to write an implementation?simple cases – yesmore complex operations – require some additional

codeBenefits: • no boilerplate for simple CRUD• full control on persisting

Page 14: Spring data presentation

Query creation

Page 15: Spring data presentation

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)

Page 16: Spring data presentation

Custom implementations1. 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:

Page 17: Spring data presentation

Configuration

Page 18: Spring data presentation

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.

Page 19: Spring data presentation

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.

Page 20: Spring data presentation

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).

Page 21: Spring data presentation

Repository populators

• Can specify data to populate storage independent format: JSON and XML

• JSON populator configuration:

• XML populator configuration:

Page 22: Spring data presentation

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.

Page 23: Spring data presentation

MongoDB supportMongoDB (from "humongous") is an open-

source 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.

Page 24: Spring data presentation

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

Page 25: Spring data presentation

Practice

Page 26: Spring data presentation

Questions

Page 27: Spring data presentation

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/

JPAhttp://www.spring-source.ru/docs_simple.php?type=manual&theme=docs_simple&docs_simple=chap03_p02

Mongohttp://docs.mongodb.org/manual/

GemFirehttp://community.gemstone.com/display/gemfire/GemFire+Tutorial

Page 28: Spring data presentation

That’s all folks

Thank you for your attention!

Page 29: Spring data presentation
Page 30: Spring data presentation
Page 31: Spring data presentation
Page 32: Spring data presentation
Page 33: Spring data presentation
Page 34: Spring data presentation

Spring Data MongoDB features• Spring configuration support using Java based @Configuration classes or an XML namespace for aMongo 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 usingMongoDB• Log4j log appender• GeoSpatial integration

Page 35: Spring data presentation

Mongo Configuration

Page 36: Spring data presentation

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.)

Page 37: Spring data presentation

MongoDB type mapping

Java Person class Document in mongo “prs” collection

Page 38: Spring data presentation

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.

Page 39: Spring data presentation

Custom Converters

Page 40: Spring data presentation

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

Page 41: Spring data presentation

Repository

Page 42: Spring data presentation

MongoDB JSON based query methods and field restriction

@org.springframework.data.mongodb.repository.Query

Simple querying:

Querying with field restriction:

Page 43: Spring data presentation

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

Page 44: Spring data presentation

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

Page 45: Spring data presentation

Miscellaneous

• Cross store support• Logging support• JMX support