Spring Data - Intro (Odessa Java TechTalks)

Post on 11-May-2015

955 views 0 download

Tags:

description

Igor Anishchenko Odessa Java TechTalks Lohika - September, 2012 This session starts with a high-level look at all that the Spring Data project has to offer. Then we’ll dive deeper into a few select Spring Data modules, including Spring Data JPA, Spring Data MongoDB and Spring Data Redis. Implementing a data access layer of an application has been cumbersome for quite a while. Too much boilerplate code had to be written! Spring Data is a project that makes it easier to build Spring-powered applications that use new data, offering a reasonably consistent programming model regardless of which type of database you choose. In addition to supporting the new “NoSQL” databases such as document and graph databases, Spring Data also greatly simplifies working with RDBMS-oriented datastores using JPA -simplifies the development of creating a JPA-based data access layer.

Transcript of Spring Data - Intro (Odessa Java TechTalks)

Igor AnishchenkoLohika - September, 2012

Spring Data

Relational Database

Clouds

Scaling

Column families

Graphs

Key Value

Documents

MongoDB

Document Database

JSON documentsJSON queries

The most important difference is the data model:

MongoDB

Mongo data model

A Mongo system (see deployment above) holds a set of databases A database holds a set of collections A collection holds a set of documents A document is a set of fields A field is a key-value pair

A key is a name (string) A value is a

basic type like string, integer, float, timestamp, binary, etc.,

a document, or an array of values

MongoDB• Flexible data model 

• Data can be inserted without a defined schema,

and the format of the data being inserted can

change at any time

• Easy scalability 

• Databases automatically spreads data across

servers, requiring no participation from the

applications.

• Servers can be added and removed without

downtime

• Transparently cache data in system

Mongo Infrastructure API

Mongo Query API

JPA?

" This document is the specification of the Java API for the management of persistence and object/relational mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.

" This document is the specification of the Java API for the management of persistence and object/relational mapping with Java EE and Java SE. The technical objective of this work is to provide an object/relational mapping facility for the Java application developer using a Java domain model to man- age a relational database.

JPA?

Decision Time?

As a developer - what are you looking for?

Spring Data

Mission statement

“… provides a familiar and consistent Spring-based programming model for NoSQL and relational stores while retaining store-specific features and capabilities”

... history• The Spring Data project was coined at

2010• Originated by Rod Johnson (SpringSource)

and Neo Technologies) early that year• They were trying to integrate the Neo4j

graph database with the Spring framework and evaluated different approaches

• Current version

Spring Data

JDBC JPAsupport for relational stores...

Spring Data

JDBC JPAsupport for relational stores...

Spring Data

JDBC JPAsupport for relational stores...

Spring Data

JDBC JPAsupport for relational stores...

Spring Data

JDBC JPAsupport for relational stores...

Building blocks of Spring Data modules

Core components

Spring Core• IoC/DI

• Spring namespace

• Configuring resources to access the stores

• Integration with core Spring functionality like JMX is provided which means that some stores will expose statistics through their native API

Mapping

• A very core part of the Spring Data modules is a mapping and conversion API that allows obtaining meta-data about domain classes

• Most of the NoSQL Java APIs do not provide support to map domain objects onto the stores data abstractions

• With native Java drivers You would usually have to write a significant amount of code to map data onto your domain objects

...Mapping

JPA - Entity mapping

Entity mapping - MongoDB

Templates

... Templates• Heavily used in spring (JdbcTemplate, JmsTemplate)

• JdbcTemplate - simplifies the use of JDBC and helps to:• avoid common errors• executes core JDBC workflow• SQL queries or updates, iteration over ResultSets and • catching JDBC exceptions

• Spring takes this concept and provided templates for noSQL stores• RedisTemplate• MongoTemplate

• Offer helper methods that allow us to execute commonly needed operations like persisting an object with a single statement while automatically taking care of appropriate resource management and exception translation

MongoTemplate usage

MongoOperation/-Template

Repositories

GenericDao

... Repositories• Provides a repository abstraction on top of the Template

implementation

• Will reduce the effort to implement data access objects to a plain interface definition for the most common scenarios like standard CRUD operations as well as executing queries in case the store supports that.

• This abstraction is actually the most top layer and blends the APIs of the different stores as much as reasonably possible.

• Interface based programming model, so you have an interface for the queries you want to trigger and then those methods will be generated without a need to implement this interface

Repositories - JPA

Repositories - MongoDB

Querydsl

DEMO

Summary• Abstraction over stores drivers• Mapping support• Templates• Repositories / custom repositories• Querydsl• Spring namespace• Cross-store persistence

?

???

? ?

Resourceswww.springframework.org/spring-data

http://github.com/SpringSource/spring-data-mongodb

O'Reilly's Open Feedback Publishing System is the book:http://ofps.oreilly.com/titles/9781449323950/

http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis