an introduction into spring data
DESCRIPTION
Slides of my talk at OOP2012.TRANSCRIPT
![Page 1: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/1.jpg)
Data Access 2.0?…please welcome…
Spring Data!
Oliver Gierke
![Page 2: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/2.jpg)
Oliver Gierke
Spring DataCore/JPA/MongoDB
![Page 3: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/3.jpg)
What to expect?
![Page 4: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/4.jpg)
Why?
How?
What?
![Page 5: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/5.jpg)
A Developer‘s View
![Page 6: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/6.jpg)
What to expect?NOT!
![Page 7: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/7.jpg)
What to expect? NOT!
![Page 8: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/8.jpg)
Overview
![Page 9: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/9.jpg)
Relational databases
![Page 10: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/10.jpg)
Scaling
![Page 11: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/11.jpg)
Data structures
![Page 12: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/12.jpg)
(No)SQLRedis
Riak
MongoDB
Cassandra
CouchDB
Neo4JHBase
SimpleDB
OrientDB
MembaseHibari Voldemort
Sones
![Page 13: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/13.jpg)
Key Value
![Page 14: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/14.jpg)
Column families
![Page 15: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/15.jpg)
Graphs
![Page 16: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/16.jpg)
Documents
![Page 17: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/17.jpg)
![Page 18: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/18.jpg)
Document databaseJSON documents
JSON queries
![Page 19: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/19.jpg)
19
MongoDB Infrastructure API
Mongo mongo = new Mongo(…);DB db = mongo.getDB("myDatabase");Collection collection = db.getCollection("myCollection");
DBObject address = new BasicDBObject();address.put("city", "London");
DBObject person = new BasicDBObject();person.put("firstname", "Dave");person.put("lastname", "Matthews");person.put("address", address);
collection.save(person);
![Page 20: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/20.jpg)
20
MongoDB Query API
Mongo mongo = new Mongo(…);DB db = mongo.getDB("myDatabase");Collection collection = db.getCollection("myCollection");
DBObject query = new BasicDBObject();query.put("address.city", "London");
DBCursor cursor = collection.find(query);
for (DBObject element : cursor) { // Map data onto object}
![Page 21: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/21.jpg)
Graph databaseNodes / Relationships
Traversals / Cypher / Gremlin
![Page 22: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/22.jpg)
22
Neo4J Infrastructure API
GraphDatabaseService database = new EmbeddedGraphDatabase(…);Transaction tx = database.beginTx();
try {Node mrAnderson = database.createNode();mrAnderson.setProperty("name", "Thomas Anderson");Node morpheus = database.createNode();morpheus.setProperty("name", "Morpheus");
Relationship friendship = mrAnderson.createRelationshipTo(morpheus, FriendTypes.KNOWS);
tx.success();} finally {
tx.finish();}
![Page 23: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/23.jpg)
23
Neo4J Query API
GraphDatabaseService database = new EmbeddedGraphDatabase(…);
CypherParser parser = new CypherParser();Query query = parser.parse("start person = Person(id = *) match " +
"person-[:colleagues]->colleague where colleague.firstname = {name}");
Map<String, Object> parameters = new HashMap<String, Object>();parameters.put("name", "Dave");
ExecutionEngine engine = new ExecutionEngine(database);ExecutionResult result = engine.execute(query, parameters);
for (EntrySet<String, Object> element : result) {// Map data onto object
}
![Page 24: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/24.jpg)
24
Neo4J entity
class Actor {
private final Node node;
public Actor(Node node) { … }
public String getName() {return (String) node.getProperty(„name“);
}
…}
![Page 25: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/25.jpg)
Forest for the woods?
![Page 26: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/26.jpg)
There‘s someSpring for that!
![Page 27: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/27.jpg)
Spring Data
![Page 28: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/28.jpg)
"… provide a familiar and consistent Spring-based programming model while retaining store-specific features and capabilities.
![Page 29: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/29.jpg)
Spring Data
JPAJDBC
![Page 30: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/30.jpg)
Spring Data
JPAJDBC
![Page 31: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/31.jpg)
Spring Data
JPAJDBC
![Page 32: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/32.jpg)
Spring Data
JPAJDBC
![Page 33: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/33.jpg)
Building blocks
![Page 34: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/34.jpg)
Spring
![Page 35: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/35.jpg)
Mapping
![Page 36: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/36.jpg)
36
JPA entity mapping
@Entityclass Person {
@Id@GeneratedValue(strategy=GenerationType.AUTO)private BigInteger id;private String firstname, lastname;
@Column(name="email") private String emailAddress;
@OneToMany private Set<Person> colleagues;
}
![Page 37: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/37.jpg)
37
Entity mapping - MongoDB
@Documentclass Person {
@Id private BigInteger id;@Indexed private String firstname, lastname;@Field("email") private String emailAddress;@DBRef private Set<Person> colleagues;
public Person(String firstname) { … }
@PersistenceConstructorpublic Person(String firstname, String lastname) { … }
…}
![Page 38: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/38.jpg)
38
Entity mapping - Neo4J
@NodeEntityclass Person {
@GraphId private long id;@Indexed private String firstname, lastname;@RelatedTo(direction = Direction.INCOMING)private Set<Person> colleagues;
public Person(String firstname) { … }
@PersistenceConstructorpublic Person(String firstname, String lastname) { … }
…}
![Page 39: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/39.jpg)
Templates
![Page 40: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/40.jpg)
40
MongoOperations / -Template
public interface MongoOperations {
// Generic callback-accepting methods <T> T execute(DbCallback<T> action);
<T> T execute(Class<?> entityClass, CollectionCallback<T> action);<T> T execute(String collectionName, CollectionCallback<T> action);
// Higher level access methods<T> List<T> find(Query query, Class<T> entityClass);void save(Object objectToSave, String collectionName);WriteResult updateFirst(Query query, Update update, Class<?>
entityClass);
// Geo API<T> GeoResults<T> geoNear(NearQuery near, Class<T> entityClass);
}
![Page 41: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/41.jpg)
41
MongoTemplate usage
// Setup infrastructureMongo mongo = new Mongo();MongoDbFactory factory = new SimpleMongoDbFactory(mongo, „foo“);MongoTemplate template = new MongoTemplate(factory);
// Create and save entityPerson dave = new Person("Dave", "Matthews");dave.setEmailAddress("[email protected]");template.save(person);
// Query entityQuery query = new Query(new Criteria("emailAddress")
.is("[email protected]"));assertThat(template.find(query), is(dave));
![Page 42: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/42.jpg)
Repositories
![Page 43: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/43.jpg)
43
Repositories - JPA
<jpa:repositories base-package="com.acme.repositories" />
public interface PersonRepository extends Repository<Person, BigInteger>{
// Finder for a single entityPerson findByEmailAddress(String emailAddress);
// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);
// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);
}
![Page 44: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/44.jpg)
44
Repositories - MongoDB
public interface PersonRepository extends Repository<Person, BigInteger>{
// Finder for a single entityPerson findByEmailAddress(String emailAddress);
// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);
// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);
// Geospatial queriesList<Person> findByLocationNear(Point location, Distance distance);GeoResults<Person> findByLocationNear(Point location);
}
![Page 45: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/45.jpg)
45
Repositories - MongoDB
<mongo:repositories base-package="com.acme.repositories" />
@Componentpublic class MyClient {
@Autowiredprivate PersonRepository repository;
public List<Person> doSomething() {
Point point = new Point(43.7, 48.8);Distance distance = new Distance(200, Metrics.KILOMETERS);return repository.findByLocationNear(point, distance);
}}
![Page 46: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/46.jpg)
46
Repositories - Neo4J
interface PersonRepository extends GraphRepository<Person, Long> // Finder for a single entityPerson findByEmailAddress(String emailAddress);
// Finder for multiple entitiesList<Person> findByLastnameLike(String lastname);
// Finder with paginationPage<Person> findByFirstnameLike(String firstname, Pageable page);
@Query("start person = Person(id = *) " + "match person-[:colleagues]->colleague where " + "colleague.firstname = {name}")List<Person> getPersonsWithColleaguesName( @Param("name") Movie m);
}
![Page 47: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/47.jpg)
Repositories
47
Querydsl
![Page 48: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/48.jpg)
48
Querydsl
QPerson $ = QPerson.person;BooleanExpression left = $.lastname.contains("eth");BooleanExpression right = $.firstname.is("Carter");
public interface QueryDslPredicateExecutor<T> {T findOne(Predicate predicate);List<T> findAll(Predicate predicate);
}
public interface PersonRepository extends Repository<Person, BigInteger>,QueryDslPredicateExecutor { … }
List<Person> result = repository.findAll(left.or(right));assertThat(result.size(), is(2));assertThat(result, hasItems(dave, carter));
![Page 49: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/49.jpg)
Wrap up
![Page 50: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/50.jpg)
Sophisticated mapping supportTemplates
RepositoriesQuerydsl
Spring namespaceGeospatial support (MongoDB)
Cross-store persistence
Wrap up
![Page 51: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/51.jpg)
Questions?
![Page 52: An introduction into Spring Data](https://reader034.vdocument.in/reader034/viewer/2022051413/554e2043b4c90571798b4c95/html5/thumbnails/52.jpg)
Resources
• www.springframework.org/spring-data
• github.com/SpringSource/spring-data-mongodb
• github.com/SpringSource/spring-data-neo4j
• http://www.se-radio.net/2010/07/episode-165-nosql-and-mongodb-with-dwight-merriman
• http://kkovacs.eu/cassandra-vs-mongodb-vs-couchdb-vs-redis