Your SlideShare is downloading. ×
An introduction into Spring Data
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

An introduction into Spring Data

4,084

Published on

Slides of my talk at OOP2012.

Slides of my talk at OOP2012.

Published in: Technology, Education
1 Comment
8 Likes
Statistics
Notes
  • Hi Oliver Gierke. Can You help me to create generic MongoRepository for all documents. Now we are using one Repository for one document class. we need only one Repository for all classes.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
4,084
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
76
Comments
1
Likes
8
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • A lot of hands on ses\n
  • \n
  • No in-depth choice advice.\n
  • Traditional approach to data storage\nDeveloper‘s point of view\n
  • Well known, solid\nAdmins know how to administer it\nProblems? Challenges?\nTwo main drivers: Cloud and Datastructures\n
  • New challenges in deployment platforms\nCloudFoundry\n
  • Consistency requirements -> hard to scale -> CAP\nSQLFire\n\n
  • Transactions as consistency switch\nOrganize data differently\n
  • Object-relational-mismatch\nInheritance, Trees (Nested Set?), Networks\n
  • Huge variety of APIs\n
  • \n
  • \n
  • \n
  • \n
  • Starring today… MongoDB\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Hard to choose\nDifferent APIs all over the place\n
  • \n
  • \n
  • Umbrella projects for Data Access 2.0\n
  • \n
  • Variety of stores supported (or planned)\n
  • Key/Value stores\n
  • Document stores\n
  • Other ones (graph)\n
  • \n
  • - Spring already provides support for JDBC, JPA, Hibernate etc.\n- add functionality on top of that\n- leverage well known Spring concepts\n - DI, AOP, Spring namespaces, JMX\n
  • POJO based mapping\nAnnotation based mapping (Morphia and beyond)\n- Persistence constructor\n- (deeply nested) Generics\n- DBRef -> embeds by default\n- Document -> specify collections\n
  • \n
  • \n
  • \n
  • Template pattern\n- Resource management\n- Exception translation\n- simple Query API\n
  • \n
  • \n
  • Eliminate boilerplate code\n\n- interface-only approach\n- finder methods\n- annotations for query tweaking\n- QueryDsl integration for type safe queries\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • JMX support\n
  • \n
  • Upcoming talks:\n- JUG BB -> Spring Data JPA\n
  • \n
  • Transcript

    • 1. Data Access 2.0? …please welcome… Spring Data! Oliver Gierke
    • 2. Oliver GierkeSpring DataCore/JPA/MongoDBogierke@vmware.comwww.olivergierke.deolivergierke
    • 3. What to expect?
    • 4. How?Why? What?
    • 5. A Developer‘s View
    • 6. What to expect? NOT!
    • 7. What to expect? NOT!
    • 8. Overview
    • 9. Relational databases
    • 10. Scaling
    • 11. Data structures
    • 12. Hibari Voldemort Membase Riak Cassandra RedisSimpleDB (No)SQL MongoDB OrientDB CouchDB HBase Sones Neo4J
    • 13. Key Value
    • 14. Column families
    • 15. Graphs
    • 16. Documents
    • 17. Document database JSON documents JSON queries
    • 18. 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);19
    • 19. 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 }20
    • 20. Graph database Nodes / RelationshipsTraversals / Cypher / Gremlin
    • 21. 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(); }22
    • 22. 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 }23
    • 23. Neo4J entity class Actor { private final Node node; public Actor(Node node) { … } public String getName() { return (String) node.getProperty(„name“); } … }24
    • 24. Forest for the woods?
    • 25. There‘s someSpring for that!
    • 26. Spring Data
    • 27. " … provide a familiar and consistent Spring-based programming model while retaining store-specific features and capabilities.
    • 28. Spring Data JDBC JPA
    • 29. Spring Data JDBC JPA
    • 30. Spring Data JDBC JPA
    • 31. Spring Data JDBC JPA
    • 32. Building blocks
    • 33. Spring
    • 34. Mapping
    • 35. 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; }36
    • 36. Entity mapping - MongoDB @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) { … } … }37
    • 37. Entity mapping - Neo4J @NodeEntity class Person { @GraphId private long id; @Indexed private String firstname, lastname; @RelatedTo(direction = Direction.INCOMING) private Set<Person> colleagues; public Person(String firstname) { … } @PersistenceConstructor public Person(String firstname, String lastname) { … } … }38
    • 38. Templates
    • 39. 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); }40
    • 40. 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));41
    • 41. Repositories
    • 42. Repositories - JPA <jpa:repositories base-package="com.acme.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); }43
    • 43. Repositories - MongoDB 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); }44
    • 44. Repositories - MongoDB <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); } }45
    • 45. Repositories - Neo4J interface PersonRepository extends GraphRepository<Person, Long> // 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); @Query("start person = Person(id = *) " + "match person-[:colleagues]->colleague where " + "colleague.firstname = {name}") List<Person> getPersonsWithColleaguesName( @Param("name") Movie m); }46
    • 46. Repositories Querydsl47
    • 47. 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));48
    • 48. Wrap up
    • 49. Wrap upSophisticated mapping support Templates Repositories Querydsl Spring namespaceGeospatial support (MongoDB) Cross-store persistence
    • 50. Questions?
    • 51. 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

    ×