34
An Introduction to Spring Data Thomas Risberg VMware [email protected] twitter: trisberg Data Access 2.0? OR Thursday, May 26, 2011

New Data Access 2.0? - springdeveloper.comspringdeveloper.com/downloads/S2G_2011_AMS_SpringData.pdf · 2011. 5. 26. · Spring Framework built-in data access support • Transaction

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

  • An Introduction to Spring Data

    Thomas RisbergVMware

    [email protected]: trisberg

    Data Access 2.0?OR

    Thursday, May 26, 2011

    mailto:[email protected]:[email protected]

  • A couple of recent trends

    • Heavyweight Databases• Heavy handed DBAs• Heavyweight App Servers

    • Lightweight NOSQL Databases• Lightweight App Servers• Cloud Deployments

    Thursday, May 26, 2011

  • Data Access 1.0

    • The relational database has been extremely successful the last 25 years

    • It was the Swiss Army knife of data access for many developers and it worked well ...

    A brief history of data access during the last quarter century

    Thursday, May 26, 2011

  • New demands on data access

    • ... until we needed inexpensive horizontal scaling for some large web based applications ...

    • ... and we needed to deploy our apps in the cloud ...

    Thursday, May 26, 2011

  • NOSQL provides specialized tools

    • Big Data/Data Warehousing- Hbase, Hadoop, Cassandra

    • Web App Backend Caching/Persistence- Redis, Riak, MongoDB

    • Social Networking Features- Neo4j

    • Disconnected Data Replication- CouchDB

    Thursday, May 26, 2011

  • Welcome to Data Access 2.0

    Thursday, May 26, 2011

  • Spring Framework built-in data access support

    • Transaction abstractions, Data access exceptions

    • JDBC - JdbcTemplate• ORM - Hibernate, JPA support• OXM - Object to XML mapping• Cache support (Spring 3.1)

    Data Access 1.0

    Thursday, May 26, 2011

  • Spring Data

    • Spring Data Redis/Riak• Spring Data Mongo/Couch• Spring Data Graph Neo4j• Spring Data Column• Spring Data Blob• Spring Data JPA Repository• Spring Data JDBC Extensions• Spring Gemfire• Spring Hadoop• Grails iNcOnSeQentiaL

    Thursday, May 26, 2011

  • What is Spring Data

    • Bring classic Spring value propositions to NOSQL and extend the SQL based support

    ✓ Productivity✓ Programming model consistency

    • Support for a wide range of NOSQL databases

    Thursday, May 26, 2011

  • Spring Data Building Blocks

    • Low level data access APIs✓ MongoTemplate, RedisTemplate ...

    • Object Mapping (Java and GORM)• Cross Store Persistence Programming model• Generic Repository support (Hades)• Productivity support in Roo and Grails

    Thursday, May 26, 2011

  • Finding Spring Data

    • GitHub: https://github.com/SpringSource

    • Web page: http://www.springsource.org/spring-data

    • Forum:http://forum.springsource.org/forumdisplay.php?f=80

    Thursday, May 26, 2011

    https://github.com/SpringSourcehttps://github.com/SpringSourcehttp://www.springsource.org/spring-datahttp://www.springsource.org/spring-datahttp://forum.springsource.org/forumdisplay.php?f=80http://forum.springsource.org/forumdisplay.php?f=80

  • Three databases for today’s talk

    • Document database

    • Relational database

    • Graph database

    Thursday, May 26, 2011

  • Three persistence strategies for today’s talk

    • Lower-level Template approach• Conventions based persistence --

    Repositories (Hades)

    • Cross-store persistence using JPA and a NOSQL database

    Thursday, May 26, 2011

  • • JSON-style documents: Lists, Maps, Primitive values• Documents organized into collections• Full or partial document updates• GridFS for efficiently storing large files• Index support – secondary and compound• Rich query language for dynamic queries• Map/Reduce• Replication and Auto Sharding

    Thursday, May 26, 2011

  • http://www.mongodb.org/

    Thursday, May 26, 2011

    http://www.mongodb.orghttp://www.mongodb.org

  • • Resource management• Callback methods

    ‣ doInSomeResource

    • Exception translation• Simple Query API

    Spring Template Pattern

    Thursday, May 26, 2011

  • 17

    Mongo DBusing MongoTemplate

    CODEhttps://github.com/trisberg/spring-data-samples/tree/

    master/data-mongo-template

    Thursday, May 26, 2011

    https://github.com/trisberg/spring-data-samples/tree/master/data-mongo-templatehttps://github.com/trisberg/spring-data-samples/tree/master/data-mongo-templatehttps://github.com/trisberg/spring-data-samples/tree/master/data-mongo-templatehttps://github.com/trisberg/spring-data-samples/tree/master/data-mongo-template

  • • Also known as HSQLDB or Hypersonic SQL• Relational Database• Table oriented data model• SQL used for queries• ... you know the rest ...

    Thursday, May 26, 2011

  • • Eliminate boilerplate code• interface-only approach• finder methods

    • findByLastName• Specifications for type safe queries• JPA CriteriaBuilder integration • QueryDsl integration

    Spring Data Repository Support

    Thursday, May 26, 2011

  • • Generates Query classes using Java APT• Code completion in IDE• Domain types and properties can be referenced

    safely

    • Adopts better to refactoring changes in domain types

    Type-safe queries for multiple backends including JPA, SQL and MongoDB in Java.

    http://www.querydsl.com/

    Thursday, May 26, 2011

    http://www.querydsl.comhttp://www.querydsl.com

  • • Repository Support✓ Spring Data JPA ✓ Spring Data Mongo

    • Spring Data JDBC Extensions

    Thursday, May 26, 2011

  • 22

    Relational database using JPA Repository

    CODEhttps://github.com/SpringSource/spring-data-jpa-examples/

    tree/master/spring-data-jpa-showcase

    Thursday, May 26, 2011

    https://github.com/SpringSource/spring-data-jpa-examples/tree/master/spring-data-jpa-showcasehttps://github.com/SpringSource/spring-data-jpa-examples/tree/master/spring-data-jpa-showcasehttps://github.com/SpringSource/spring-data-jpa-examples/tree/master/spring-data-jpa-showcasehttps://github.com/SpringSource/spring-data-jpa-examples/tree/master/spring-data-jpa-showcase

  • • DB is a collection of graph nodes, relationships• Nodes and relationships have properties• Querying is done via a traversal API• Indexes on node/relationship properties• Written in Java, can be embedded• Transactional (ACID)• Master-Slave replication• Standalone using REST API

    Thursday, May 26, 2011

  • Spring Data Graph Neo4j

    • Using AspectJ support we are providing a new programming model:

    • Use annotations to define POJO entities• Constructor advice automatically handles entity

    creation

    • Entity field state persisted to graph using aspects• Leverage graph database APIs from POJO model• Annotation-driven indexing of entities for search

    Thursday, May 26, 2011

  • Spring Data Graph Neo4j Cross-store

    • JPA data and “NOSQL” data can share a data model• Separate the persistence provider by using

    annotations– could be the entire Entity– or, some of the fields of an Entity

    • We call this cross-store persistence– One transaction manager to coordinate the “NOSQL” store

    with the JPA relational database– AspectJ support to manage the “NOSQL” entities and

    fields• holds on to changed values in “change sets” until the

    transaction commits for non-transactional data stores

    Thursday, May 26, 2011

  • A cross-store scenario ...

    You have a traditional web app using JPA to persist data to a relational database ...

    Thursday, May 26, 2011

  • JPA Data Model

    Restaurant@Entitypublic class Restaurant {

    @Id @GeneratedValue private Long id; private String name; private String city; private String state; private String zipCode;

    UserAccount@Entity@Table(name = "user_account")public class UserAccount { @Id @GeneratedValue private Long id; private String userName; private String firstName; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set favorites;

    Thursday, May 26, 2011

  • Cross-store Data Model

    Recommendation

    @RelationshipEntitypublic class Recommendation { @StartNode private UserAccount user; @EndNode private Restaurant restaurant; private int stars; private String comment;

    Restaurant@Entity@NodeEntity(partial = true)public class Restaurant { @Id @GeneratedValue private Long id; private String name; private String city; private String state; private String zipCode;

    UserAccount@Entity@Table(name = "user_account")@NodeEntity(partial = true)public class UserAccount { @Id @GeneratedValue private Long id; private String userName; private String firstName; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set favorites;

    @GraphProperty String nickname; @RelatedTo(type = "friends", elementClass = UserAccount.class) Set friends; @RelatedToVia(type = "recommends", elementClass = Recommendation.class) Iterable recommendations;

    Thursday, May 26, 2011

  • Cross-store Data Model

    Recommendation

    @RelationshipEntitypublic class Recommendation { @StartNode private UserAccount user; @EndNode private Restaurant restaurant; private int stars; private String comment;

    Restaurant@Entity@NodeEntity(partial = true)public class Restaurant { @Id @GeneratedValue private Long id; private String name; private String city; private String state; private String zipCode;

    UserAccount@Entity@Table(name = "user_account")@NodeEntity(partial = true)public class UserAccount { @Id @GeneratedValue private Long id; private String userName; private String firstName; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set favorites;

    @GraphProperty String nickname; @RelatedTo(type = "friends", elementClass = UserAccount.class) Set friends; @RelatedToVia(type = "recommends", elementClass = Recommendation.class) Iterable recommendations;

    Thursday, May 26, 2011

  • Cross-store Data Model

    Recommendation

    @RelationshipEntitypublic class Recommendation { @StartNode private UserAccount user; @EndNode private Restaurant restaurant; private int stars; private String comment;

    Restaurant@Entity@NodeEntity(partial = true)public class Restaurant { @Id @GeneratedValue private Long id; private String name; private String city; private String state; private String zipCode;

    UserAccount@Entity@Table(name = "user_account")@NodeEntity(partial = true)public class UserAccount { @Id @GeneratedValue private Long id; private String userName; private String firstName; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set favorites;

    @GraphProperty String nickname; @RelatedTo(type = "friends", elementClass = UserAccount.class) Set friends; @RelatedToVia(type = "recommends", elementClass = Recommendation.class) Iterable recommendations;

    Thursday, May 26, 2011

  • Cross-store Data Model

    Recommendation

    @RelationshipEntitypublic class Recommendation { @StartNode private UserAccount user; @EndNode private Restaurant restaurant; private int stars; private String comment;

    Restaurant@Entity@NodeEntity(partial = true)public class Restaurant { @Id @GeneratedValue private Long id; private String name; private String city; private String state; private String zipCode;

    UserAccount@Entity@Table(name = "user_account")@NodeEntity(partial = true)public class UserAccount { @Id @GeneratedValue private Long id; private String userName; private String firstName; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set favorites;

    @GraphProperty String nickname; @RelatedTo(type = "friends", elementClass = UserAccount.class) Set friends; @RelatedToVia(type = "recommends", elementClass = Recommendation.class) Iterable recommendations;

    Thursday, May 26, 2011

  • Cross-store Data Model

    Recommendation

    @RelationshipEntitypublic class Recommendation { @StartNode private UserAccount user; @EndNode private Restaurant restaurant; private int stars; private String comment;

    Restaurant@Entity@NodeEntity(partial = true)public class Restaurant { @Id @GeneratedValue private Long id; private String name; private String city; private String state; private String zipCode;

    UserAccount@Entity@Table(name = "user_account")@NodeEntity(partial = true)public class UserAccount { @Id @GeneratedValue private Long id; private String userName; private String firstName; private String lastName; @Temporal(TemporalType.TIMESTAMP) private Date birthDate; @ManyToMany(cascade = CascadeType.ALL) private Set favorites;

    @GraphProperty String nickname; @RelatedTo(type = "friends", elementClass = UserAccount.class) Set friends; @RelatedToVia(type = "recommends", elementClass = Recommendation.class) Iterable recommendations;

    Thursday, May 26, 2011

  • 29

    Cross-store persistence with JPA/Neo4j

    CODEhttps://github.com/SpringSource/spring-data-graph-examples/tree/

    1.0.0.RELEASE/myrestaurants-social

    Thursday, May 26, 2011

    https://github.com/SpringSource/spring-data-graph-examples/tree/1.0.0.M1/myrestaurants-socialhttps://github.com/SpringSource/spring-data-graph-examples/tree/1.0.0.M1/myrestaurants-socialhttps://github.com/SpringSource/spring-data-graph-examples/tree/1.0.0.M1/myrestaurants-socialhttps://github.com/SpringSource/spring-data-graph-examples/tree/1.0.0.M1/myrestaurants-social

  • Questions?

    http://www.sxc.hu/photo/860327

    Thursday, May 26, 2011

    http://www.sxc.hu/photo/860327http://www.sxc.hu/photo/860327