Upload
oliver-gierke
View
3.131
Download
5
Tags:
Embed Size (px)
DESCRIPTION
Introduction into Spring Data, SpringOne 2011
Citation preview
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
An Introduction to Spring Data
Oliver Gierke - SpringSource, a division of VMware
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Oliver Gierke
Spring DataCore/JPA/MongoDB
2
What to expect?
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Why?
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Why?
How?
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Why?
How?
What?
5
A Developer‘s View
What to expect?NOT!
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
What to expect? NOT!
7
Retrospect
Relational databases
Cloud
Scaling
Data structures
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
(No)SQLRedis
Riak
MongoDB
Cassandra
CouchDB
Neo4JHBase
SimpleDB
OrientDB
MembaseHibari Voldemort
Sones
Graphs
Documents
Column families
Key Value
Forest for the woods?
19
A Developer‘s View
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
There‘s someSpring for that!
Spring Data
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
"… provide a familiar and consistent Spring-based programming model while not over-abstracting custom traits of the specific store.
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
JPAJDBC
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
JPAJDBC
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
JPAJDBC
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
JPAJDBC
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
JPAJDBC
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
JPAJDBC
Building blocks
Spring
Mapping
32
Entity mapping
@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) { … }
…}
Templates
34
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);
}
35
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));
Repositories
37
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);
// Geospatial queriesList<Person> findByLocationNear(Point location, Distance distance);GeoResults<Person> findByLocationNear(Point location);
}
38
Repositories usage
<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);
}}
Repositories
39
Querydsl
40
MongoTemplate usage
public interface QueryDslPredicateExecutor<T> {T findOne(Predicate predicate);List<T> findAll(Predicate predicate);
}
public interface PersonRepository extends Repository<Person, ObjectId>,QueryDslPredicateExecutor { … }
QPerson $ = QPerson.person;BooleanExpression left = $.lastname.contains(„eth“);BooleanExpression right = $.firstname.is(„Carter“);
List<Person> result = repository.findAll(left.or(right));assertThat(result.size(), is(2));assertThat(result, hasItems(dave, carter));
JPA
42
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;
}
43
Repository
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);
}
<jpa:repositories base-package=„com.acme.repositories“ />
Wrap up
• Sophisticated mapping support• Templates• Repositories• Querydsl• Spring namespace• Geospatial support• Cross-store persistence
45
Wrap up
46
Upcoming Talks
• Today– 10:15 - Spring Data Gemfire– 12:45 - Spring Data MongoDB & CloudFoundry– 4:30 - Spring Data Neo4J
• Thursday– 8:30 - Spring Data JPA & Repositories– 10:15 - Polyglot persistence
• Friday– 8:30 - SQLFire
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Play with it
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Participate
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
Spread the word
Questions?
51
Resources
• www.springframework.org/spring-data• github.com/SpringSource/spring-data-mongodb• 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
• Building blocks - http://www.sxc.hu/photo/297189• Columns - http://www.sxc.hu/photo/1033540• Dilemma - http://www.sxc.hu/photo/125069• Forest - http://www.sxc.hu/photo/1274066• Glasses - http://www.sxc.hu/photo/696003• Mapping - http://www.sxc.hu/photo/1253374• Nails - http://www.sxc.hu/photo/933587• Questions - http://www.sxc.hu/photo/860327• Repository - http://www.sxc.hu/photo/1042408• Retrospect - http://www.sxc.hu/photo/921297• Spreadsheet - http://www.sxc.hu/photo/338505• Spring - http://www.sxc.hu/photo/1291358• Umbrella - http://www.sxc.hu/photo/1364634• Template - http://www.sxc.hu/photo/619819• Wrap up - http://www.sxc.hu/photo/922227
52
Sources