Contenu connexe Similaire à An Introduction to Spring Data (20) Plus de Oliver Gierke (14) An Introduction to Spring Data1. An Introduction to Spring Data
Oliver Gierke - SpringSource, a division of VMware
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
2. Oliver Gierke
Spring Data
Core/JPA/MongoDB
ogierke@vmware.com
www.olivergierke.de
olivergierke
2 © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
3. What to expect?
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
5. How?
Why?
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
6. How?
Why?
What?
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
8. What to expect?
NOT!
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
15. Hibari Voldemort
Membase
Riak Cassandra
Redis
SimpleDB (No)SQL MongoDB
OrientDB CouchDB
HBase
Sones
Neo4J
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
22. There‘s some
Spring for that!
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
24. " … 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.
25. JDBC JPA
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
26. JDBC JPA
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
27. JDBC JPA
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
28. JDBC JPA
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
29. JDBC JPA
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
30. JDBC JPA
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
34. Entity mapping
@Document
class Person {
@Id private BigInteger id;
@Indexed private String firstname, lastname;
@Field(„email“) private String emailAddress;
@DBRef private Set<Person> colleagues;
public Person(String firstname) { … }
@PersistenceConstructor
public Person(String firstname, String lastname) { … }
…
}
32
36. 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);
}
34
37. MongoTemplate usage
// Setup infrastructure
Mongo mongo = new Mongo();
MongoDbFactory factory = new SimpleMongoDbFactory(mongo, „foo“);
MongoTemplate template = new MongoTemplate(factory);
// Create and save entity
Person dave = new Person(„Dave“, „Matthews“);
dave.setEmailAddress(„dave@dmband.com“);
template.save(person);
// Query entity
Query query = new Query(new Criteria(„emailAddress“)
.is(„dave@dmband.com“));
assertThat(template.find(query), is(dave));
35
39. Repositories
public interface PersonRepository extends Repository<Person, BigInteger>
{
// Finder for a single entity
Person findByEmailAddress(String emailAddress);
// Finder for multiple entities
List<Person> findByLastnameLike(String lastname);
// Finder with pagination
Page<Person> findByFirstnameLike(String firstname, Pageable page);
// Geospatial queries
List<Person> findByLocationNear(Point location, Distance distance);
GeoResults<Person> findByLocationNear(Point location);
}
37
40. Repositories usage
<mongo:repositories base-package=„com.acme.repositories“ />
@Component
public class MyClient {
@Autowired
private 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);
}
}
38
42. 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));
40
44. JPA entity mapping
@Entity
class Person {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private BigInteger id;
private String firstname, lastname;
@Column(name=„email“)
private String emailAddress;
@OneToMany
private Set<Person> colleagues;
}
42
45. Repository
public interface PersonRepository extends Repository<Person, BigInteger>
{
// Finder for a single entity
Person findByEmailAddress(String emailAddress);
// Finder for multiple entities
List<Person> findByLastnameLike(String lastname);
// Finder with pagination
Page<Person> findByFirstnameLike(String firstname, Pageable page);
}
<jpa:repositories base-package=„com.acme.repositories“ />
43
47. Wrap up
• Sophisticated mapping support
• Templates
• Repositories
• Querydsl
• Spring namespace
• Geospatial support
• Cross-store persistence
45
48. 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
46
49. Play with it
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
51. Spread the word
© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
54. Sources
• 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