DevEX - reference for building teams, processes, and platforms
Spring Data NHJUG April 2012
1. Spring Data
Thomas Risberg
VMware
trisberg@vmware.com
Tuesday, April 17, 12 1
2. •expert in NO SQL • committer on Spring Framework
• currently works for VMware’s Cloud • former Oracle DBA
Foundry team on framework integration • former Java J2EE developer
• member of Spring Data team email: trisberg@vmware.com
twitter: @trisberg
Tuesday, April 17, 12 2
4. New demands on data access ...
• massive amounts of data • social networking features
• structured and • inexpensive horizontal scaling
unstructured data • deploying apps in the cloud
• real-time analysis • ...
image courtesy of Bitcurrent
Tuesday, April 17, 12 4
5. New types of data stores ...
Column/
• Redis
• Cassandra
• Riak
• HBase
• MongoDB
• CouchDB • Neo4j
Hadoop
Tuesday, April 17, 12 5
6. 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)
Tuesday, April 17, 12 6
7. Spring Data
• Bring classic Spring benefits to all databases
✓ Productivity
✓ Programming model consistency
• Conventions based generic repository support
• Mapping between Java domain objects and data
store
• Support for a wide range of new databases
Tuesday, April 17, 12 7
8. Spring Data sub-projects
• JPA Repository
• JDBC Extensions
• Redis (Key Value)
• MongoDB (Document)
• Neo4j (Graph)
• Gemfire (Data Grid)
• Apache Hadoop (Big Data)
• Riak (Key Value - in development)
• Cassandra (Column Store - planned)
Tuesday, April 17, 12 8
9. Spring Data Building
Blocks
• Low level data access APIs
✓ MongoTemplate, RedisTemplate ...
• Object Mapping (Java to Datastore)
• Generic Repository support
• Cross Store Persistence Programming
model
Tuesday, April 17, 12 9
10. 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
Tuesday, April 17, 12 10
11. Three databases for
today’s talk
• Relational database
• Document database
• Graph database
Tuesday, April 17, 12 11
12. Three persistence
strategies for today’s talk
• Conventions based persistence --
Repositories
• Lower-level Template approach
• Cross-store persistence using JPA and
a non-relational database
Tuesday, April 17, 12 12
13. Repository
http://www.sxc.hu/photo/1020163
Tuesday, April 17, 12 13
14. Repository
Mediates between the domain and data mapping layers using
a collection-like interface for accessing domain objects.
http://martinfowler.com/eaaCatalog/repository.html
http://msdn.microsoft.com/en-us/library/ff649690.aspx
Tuesday, April 17, 12 14
15. Spring Data
Repository
• Generic repository implementation
• Basic CRUD (create, read, update and
delete) methods
• Generating code for dynamic find methods
defined in repository interface like
findByName, findByAgeBetween etc.
• Pagination and sorting support
• Currently JPA, Mongo and Neo4j
implementations
Tuesday, April 17, 12 15
16. Repository<T, ID extends Serializable>
CrudRepository<T, ID extends Serializable>
PagingAndSortingRepository<T, ID extends Serializable>
Tuesday, April 17, 12 16
33. Getting started with
$ wget http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.4.tgz
$ tar xvzf mongodb-osx-x86_64-2.0.4.tgz
$ cd mongodb-osx-x86_64-2.0.4
$ ./bin/mongod --dbpath /Users/trisberg/Data/mongodb/
Sun Apr 15 11:33:11 [initandlisten] MongoDB starting : pid=34692 port=27017 dbpath=/Users/trisberg/Data/
mongodb/ 64-bit host=Montserrat.local
Sun Apr 15 11:33:11 [initandlisten] db version v2.0.4, pdfile version 4.5
Sun Apr 15 11:33:11 [initandlisten] git version: 329f3c47fe8136c03392c8f0e548506cb21f8ebf
Sun Apr 15 11:33:11 [initandlisten] build info: Darwin erh2.10gen.cc 9.8.0 Darwin Kernel Version 9.8.0:
Wed Jul 15 16:55:01 PDT 2009; root:xnu-1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40
Sun Apr 15 11:33:11 [initandlisten] options: { dbpath: "/Users/trisberg/Data/mongodb/" }
Sun Apr 15 11:33:11 [initandlisten] journal dir=/Users/trisberg/Data/mongodb/journal
Sun Apr 15 11:33:11 [initandlisten] recover : no journal files present, no recovery needed
Sun Apr 15 11:33:12 [websvr] admin web console waiting for connections on port 28017
Sun Apr 15 11:33:12 [initandlisten] waiting for connections on port 27017
Tuesday, April 17, 12 33
34. &
$ vmc create-service mongodb mongo-books
$ vmc tunnel mongo-books mongo
Password: *****
Binding Service [mongo-books]: OK
Stopping Application: OK
Staging Application: OK
Starting Application: OK
Getting tunnel connection info: OK
Service connection info:
username : 97da4cc2-8919-42f9-8bdb-11f0e565c2b6
password : 61b04646-4f9f-4804-8360-20662c2a2c95
name : db
Starting tunnel to mongo-books on port 10000.
Launching 'mongo --host localhost --port 10000 -u 97da4cc2-8919-42f9-8bdb-11f0e565c2b6 -p
61b04646-4f9f-4804-8360-20662c2a2c95 db'
MongoDB shell version: 2.0.1
connecting to: localhost:10000/db
> show collections
author
book
system.indexes
system.users
> db.author.find()
{ "_id" : ObjectId("4f8af65d942bb7cb40b29764"), "_class" : "org.springframework.data.demo.domain.Author",
"name" : "Craig Walls" }
{ "_id" : ObjectId("4f8af65d942bb7cb40b29765"), "_class" : "org.springframework.data.demo.domain.Author",
"name" : "Kristina Chodorow" }
...
Tuesday, April 17, 12 34
35. Spring Data MongoDB
✓ MongoTemplate
• MongoConverter interface for mapping Mongo documents
• MappingMongoConverter for POJO mapping support,
leveraging Spring TypeConverters
• Annotation based mapping (@Document, @Id, @DbRef)
• Connection affinity callbacks
• Exception translation
• Support for GridFS and Map Reduce
✓ MongoRepository
• Similar support as JPA Repositories
Tuesday, April 17, 12 35
37. r
Fo
lt
ui
B
<beans profile="default">
<mongo:db-factory host="localhost" port="27017" dbname="db"/>
</beans>
<beans profile="cloud">
<cloud:mongo-db-factory id="mongoDbFactory" write-concern="NORMAL"/>
</beans>
Tuesday, April 17, 12 37
38. Document store using
MongoTemplate
LIVE CODE
https://github.com/trisberg/mongo-bookshelf-template
Tuesday, April 17, 12 38
39. • 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
Tuesday, April 17, 12 39
41. Features
• Support for property graphs (nodes connected via
relationships, each with arbitrary properties)
• Transparent mapping of annotated POJO entities (using
AspectJ
• Neo4jTemplate with convenient API, exception translation
and optional transaction management
• Supports the Cypher and Gremlin query languages
• Dynamic type projections (duck typing)
• Spring Data Repositories Support
• Cross-store support for partial JPA – Graph Entities
Tuesday, April 17, 12 41
42. 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
Tuesday, April 17, 12 42
43. A cross-store scenario ...
You have a traditional web app using JPA to persist data to
a relational database
Tuesday, April 17, 12 43
44. JPA Data Model
Restaurant UserAccount
@Entity @Entity
public class Restaurant { @Table(name = "user_account")
@Id @GeneratedValue public class UserAccount {
private Long id; @Id @GeneratedValue
private String name; private Long id;
private String city; private String userName;
private String state; private String firstName;
private String zipCode; private String lastName;
@Temporal(TemporalType.TIMESTAMP)
private Date birthDate;
@ManyToMany(cascade = CascadeType.ALL)
private Set<Restaurant> favorites;
Tuesday, April 17, 12 44
45. Cross-store
Data Model
Restaurant UserAccount
@Entity @Entity
@NodeEntity(partial = true) @Table(name = "user_account")
public class Restaurant { @NodeEntity(partial = true)
@Id @GeneratedValue public class UserAccount {
private Long id; @Id @GeneratedValue
private String name; private Long id;
private String city; private String userName;
private String state; private String firstName;
private String zipCode; private String lastName;
@Temporal(TemporalType.TIMESTAMP)
private Date birthDate;
@ManyToMany(cascade = CascadeType.ALL)
Recommendation private Set<Restaurant> favorites;
@RelationshipEntity @GraphProperty
public class Recommendation { String nickname;
@StartNode @RelatedTo(type = "friends",
private UserAccount user; elementClass = UserAccount.class)
@EndNode Set<UserAccount> friends;
private Restaurant restaurant; @RelatedToVia(type = "recommends",
private int stars; elementClass = Recommendation.class)
private String comment; Iterable<Recommendation> recommendations;
Tuesday, April 17, 12 45
46. Cross-store persistence
with JPA/Neo4j
DEMO
https://github.com/SpringSource/spring-data-neo4j/tree/
2.0.1.RELEASE/spring-data-neo4j-examples/myrestaurants-social
Tuesday, April 17, 12 46
47. Questions?
http://www.sxc.hu/photo/860327
Tuesday, April 17, 12 47