An Introduction to Spring Data

3,793 views

Published on

Introduction into Spring Data, SpringOne 2011

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,793
On SlideShare
0
From Embeds
0
Number of Embeds
85
Actions
Shares
0
Downloads
105
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

An Introduction to Spring Data

  1. 1. An Introduction to Spring DataOliver Gierke - SpringSource, a division of VMware© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  2. 2. Oliver Gierke Spring Data Core/JPA/MongoDB ogierke@vmware.com www.olivergierke.de olivergierke2 © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  3. 3. What to expect?© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  4. 4. Why?© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  5. 5. How? Why?© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  6. 6. How? Why? What?© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  7. 7. A Developer‘s View5
  8. 8. What to expect? NOT!© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  9. 9. What to expect? NOT!7
  10. 10. Retrospect
  11. 11. Relational databases
  12. 12. Cloud
  13. 13. Scaling
  14. 14. Data structures
  15. 15. Hibari Voldemort Membase Riak Cassandra RedisSimpleDB (No)SQL MongoDB OrientDB CouchDB HBase Sones Neo4J © 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  16. 16. Graphs
  17. 17. Documents
  18. 18. Column families
  19. 19. Key Value
  20. 20. Forest for the woods?
  21. 21. A Developer‘s View19
  22. 22. There‘s some Spring for that!© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  23. 23. Spring Data
  24. 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. 25. JDBC JPA© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  26. 26. JDBC JPA© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  27. 27. JDBC JPA© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  28. 28. JDBC JPA© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  29. 29. JDBC JPA© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  30. 30. JDBC JPA© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  31. 31. Building blocks
  32. 32. Spring
  33. 33. Mapping
  34. 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
  35. 35. Templates
  36. 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. 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
  38. 38. Repositories
  39. 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. 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
  41. 41. Repositories Querydsl39
  42. 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
  43. 43. JPA
  44. 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. 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
  46. 46. Wrap up
  47. 47. Wrap up• Sophisticated mapping support• Templates• Repositories• Querydsl• Spring namespace• Geospatial support• Cross-store persistence45
  48. 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 - SQLFire46
  49. 49. Play with it© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  50. 50. Participate© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  51. 51. Spread the word© 2011 SpringOne 2GX 2011. All rights reserved. Do not distribute without permission.
  52. 52. Questions?
  53. 53. 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-redis51
  54. 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/92222752

×