Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring Data Repositories - A Deep Dive

Spring Data Repositories - A Deep Dive

Slides of my talk on Spring Data repositories at 33degree conference in Warszaw, Poland.

Oliver Drotbohm

March 14, 2013
Tweet

More Decks by Oliver Drotbohm

Other Decks in Programming

Transcript

  1. Spring Data
    Repositories
    A Deep Dive
    Oliver Gierke

    View Slide

  2. Oliver Gierke
    SpringSource
    Spring Data modules
    Core / JPA / Mongo
    [email protected]
    www.olivergierke.de
    olivergierke

    View Slide

  3. Hands on

    View Slide

  4. Sample code
    https://github.com/olivergierke/
    repositories-deepdive

    View Slide

  5. Step 0
    Initial project setup

    View Slide

  6. Summary
    Use kickstart projects to ease setup

    View Slide

  7. Step 1
    Basic JPA infrastructure setup

    View Slide

  8. "Persistence technology of choice is JPA.
    e application uses JavaConfig and
    sample data contained in data.sql.

    View Slide

  9. Summary
    Easy setup through JavaConfig
    XML-less JPA configuration

    View Slide

  10. Step 2
    Quickstart

    View Slide

  11. "e implementation of the persistence
    layer will be based on the Spring Data
    repositories abstraction. Customers can
    be saved, looked up by their id, email
    address.

    View Slide

  12. Summary
    Interface-based programming model
    No implementation required
    Queries derived from method names

    View Slide

  13. Step 3
    Extended CRUD methods

    View Slide

  14. " Customers can be deleted and obtained all
    at once.

    View Slide

  15. Summary
    Switched to CrudRepository
    Exposed CRUD methods
    Broad API exposed

    View Slide

  16. Step 4
    Pagination

    View Slide

  17. "Customers can be accessed page by page.

    View Slide

  18. Summary
    Switched to PagingAndSortingRepository
    Exposed CRUD methods and paging ones
    Broad API exposed

    View Slide

  19. Step 5
    Re-declaring existing CRUD methods

    View Slide

  20. "CustomerRepository.findAll() should
    rather return a List. e transaction
    timeout for save(…) should be
    customized to 10 seconds.

    View Slide

  21. Summary
    Re-declare methods to customize
    Return types
    Annotation config (Tx, Locking)

    View Slide

  22. Step 6
    Introducing a read-only repository base
    interface

    View Slide

  23. "Products shall be accessible in read-only
    mode only.

    View Slide

  24. Summary
    Craft custom base interface
    Return types
    Annotation config (Tx, Locking)

    View Slide

  25. Step 7
    Using manually defined queries

    View Slide

  26. "As a user, I want to look up products by
    their custom attributes.

    View Slide

  27. Summary
    @Query annotation
    JPA named queries
    Spring Data named queries

    View Slide

  28. Step 8
    Flexible predicate execution

    View Slide

  29. "As a user, I want to search for customers
    by firstname, lastname, email address
    and any combination of them

    View Slide

  30. Summary
    Querydsl - type safe queries for Java
    QuerydslPredicateExecutor

    View Slide

  31. Side track:
    Repository proxies

    View Slide

  32. Proxy mechanism
    Using Spring's JDK proxy support
    Bootstrap through Factory
    Spring FactroyBean / CDI
    XML namespace / @EnableRepositories

    View Slide

  33. Proxy mechanism
    Query interface
    CRUD implementation class
    QueryDslPredicateExecutor
    Custom implementation extension

    View Slide

  34. Step 9
    Custom implementations for repositories

    View Slide

  35. "As an admin user, I'd like to delete all
    products beyond a given price.

    View Slide

  36. Summary
    Provide custom implementation
    Base class support (Querydsl)

    View Slide

  37. Stuff on top
    Spring MVC integration
    Spring Data REST
    Spring Roo

    View Slide

  38. Up and beyond

    View Slide

  39. Supported
    modules
    JPA
    MongoDB
    Neo4j
    Gemfire

    View Slide

  40. Community
    implementations
    Spring Data Solr
    Couchbase
    Elasticsearch
    FuzzyDB

    View Slide

  41. Summary

    View Slide

  42. Interface-based
    programming
    model

    View Slide

  43. Start simple, get
    more sophisticated

    View Slide

  44. Declarative
    query execution

    View Slide

  45. Flexible predicate
    execution

    View Slide

  46. Custom
    implementation

    View Slide

  47. CDI integration

    View Slide

  48. Spring Data
    Modern Data Access For Enterprise Java
    NoSQL
    JPA
    JDBC
    Redis
    Big Data Hadoop
    HBase
    MongoDB Neo4j REST exporter
    Roo
    Hive
    Pig
    Querydsl Repositories
    Gemfire
    Splunk
    Free JPA chapter - http://bit.ly/sd-book-chapter

    View Slide

  49. Spring Data Book
    http://bit.ly/sd-book

    View Slide

  50. Questions?

    View Slide

  51. Thank you!

    View Slide

  52. Code
    https://github.com/olivergierke/
    repositories-deepdive
    https://github.com/olivergierke/
    spring-restbucks

    View Slide