Spring data presentation

Post on 11-May-2015

1.201 views 3 download

Tags:

description

Presentation about main features about framework.

Transcript of Spring data presentation

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

Repository

Template

Object Mapping

Datastore

Client

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

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 mappingGemFire

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 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:

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

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 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:

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

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/

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

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

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