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

Spring Data Repositories - Best practices

Spring Data Repositories - Best practices

Slides of my webinar on Spring Data Repositories best practices. The demo slides contain direct links to the demo snippets in the webinar recording.



Oliver Drotbohm

February 18, 2014


  1. Oliver Gierke & Thomas Darimont Spring Data Repositories Best Practices

    © 2014 Pivotal
  2. !2 Oliver Gierke ! Spring Data Project Lead JPA 2.1

    Expert Group ! ogierke@gopivotal.com www.olivergierke.de olivergierke
  3. !3 Thomas Darimont ! Spring Data Engineer JPA / MongoDB

    / Redis ! tdarimont@gopivotal.com www.tutorials.de thomasdarimont
  4. Hands on !4

  5. Sample code http:/ /bit.ly/repositories-deepdive-webinar ! Branch: webinar2014 !5

  6. Step 0 Initial project setup !6

  7. !7 "How to get a Spring Data JPA based project

    up and running quickly?
  8. Demo Check out Spring Boot Easily configure dependencies for your

    project Defaults application config based on classpath ! Jump to the video !8
  9. Step 1 Basic JPA infrastructure setup !9

  10. !10 "Persistence technology of choice is JPA. The application uses

    JavaConfig and sample data contained in data.sql.
  11. Demo Easy setup through JavaConfig XML-less JPA configuration ! Jump

    to the video !11
  12. Step 2 Quick start !12

  13. !13 "The 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.
  14. Demo Interface-based programming model No implementation required Queries derived from

    method names ! Jump to the video !14
  15. Step 3 Extended CRUD methods !15

  16. !16 "Customers can be deleted and obtained all at once.

  17. Demo Switched to CrudRepository Exposed CRUD methods Broad API exposed

    ! Jump to the video !17
  18. Step 4 Pagination !18

  19. !19 "Customers can be accessed page by page.

  20. Demo Switched to PagingAndSortingRepository Exposed CRUD methods and paging ones

    Broad API exposed ! Jump to the video !20
  21. Step 5 Re-declaring existing CRUD methods !21

  22. !22 "CustomerRepository.findAll() should rather return a List. The transaction timeout

    for save(…) should be
 customized to 10 seconds.
  23. Demo Re-declare methods to customize Return types Annotation config (Tx,

    Locking, Query, Hints) ! Jump to the video !23
  24. Step 6 Introducing a read-only repository base interface !24

  25. !25 "Products shall be accessible in read-only mode only.

  26. Demo Craft custom base interface Narrow down the API to

    the necessary parts ! Jump to the video !26
  27. Step 7 Using manually defined queries !27

  28. !28 "As a user, I want to look up products

    by their custom attributes.
  29. Demo @Query annotation JPA named queries Spring Data named queries

    ! Jump to the video !29
  30. Step 8 Flexible predicate execution !30

  31. !31 "As a user, I want to search for customers

    by first name, last name, email address and any combination of them
  32. Demo Querydsl - type safe queries for Java QuerydslPredicateExecutor !

    Jump to the video !32
  33. Step 9 Custom implementations for repositories !33

  34. !34 "As an admin user, I'd like to use custom

    code to delete all products beyond a 
 given price.
  35. Demo Provide custom implementation Base class support (Querydsl) ! Jump

    to the video !35
  36. Side track: Repository proxies !36

  37. Proxy mechanism Using Spring's JDK proxy support Bootstrap through Factory

    Spring FactoryBean / CDI XML namespace / @EnableRepositories !37
  38. Proxy mechanism Query interface CRUD implementation class QueryDslPredicateExecutor Custom implementation

    extension !38
  39. Stuff on top Spring MVC integration Spring Data REST Spring

    Boot !39
  40. Up and beyond !40

  41. Supported modules JPA MongoDB Neo4j Gemfire !41

  42. Community modules Solr Elasticsearch Couchbase FuzzyDB Cassandra !42

  43. Summary !43

  44. Interface-based programming model !44

  45. Start simple, get more sophisticated !45

  46. Declarative query execution !46

  47. Flexible predicate execution !47

  48. Custom implementation !48

  49. CDI integration !49

  50. Resources !50

  51. !51 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
  52. !52 Get More Refcardz! Visit refcardz.com #184 By: Oliver Gierke

    ABOUT THE SPRING DATA PROJECT The Spring Data project is part of the ecosystem surrounding the Spring Framework and constitutes an umbrella project for advanced data access related topics. It contains modules to support traditional relational data stores (based on plain JDBC or JPA), NoSQL ones (like MongoDB, Neo4j or Redis), and big data technologies like Apache Hadoop. The core mission of the project is to provide a familiar and consistent Spring-based programming model for various data access technologies while retaining VWRUHVSHFLͤFIHDWXUHVDQGFDSDELOLWLHV General Themes ,QIUDVWUXFWXUH&RQͤJXUDWLRQ6XSSRUW $FRUHWKHPHRIDOOWKH6SULQJ'DWDSURMHFWVLVVXSSRUWIRUFRQͤJXULQJ resources to access the underlying technology. This support is implemented using XML namespaces and support classes for Spring -DYD&RQͤJDOORZLQJ\RXWRHDVLO\VHWXSDFFHVVWRD0RQJRGDWDEDVHDQ embedded Neo4j instance, and the like. Also, integration with core Spring functionality like JMX is provided, meaning that some stores will expose statistics through their native API, which will be exposed to JMX via Spring Data. 2EMHFW0DSSLQJ)UDPHZRUN Most of the NoSQL Java APIs do not provide support to map domain objects onto the stores’ data model (e.g., documents in MongoDB, or nodes and relationships for Neo4j). So, when working with the native Java GULYHUV\RXZRXOGXVXDOO\KDYHWRZULWHDVLJQLͤFDQWDPRXQWRIFRGHWR map data onto the domain objects of your application when reading, and vice versa on writing. Thus, a core part of the Spring Data project is a mapping and conversion API that allows obtaining metadata about domain classes to be persisted and enables the conversion of arbitrary domain REMHFWVLQWRVWRUHVSHFLͤFGDWDW\SHV 7HPSODWH$3,V 2QWRSRIWKHREMHFWPDSSLQJ$3,ZH̵OOͤQGRSLQLRQDWHG$3,VLQWKHIRUP of template pattern implementations already well-known from Spring’s JPA XML element Description <jpa:repositories /> Enables Spring Data repositories support for repository interfaces underneath the package configured in the base-package attribute. JavaConfig equivalent is @EnableJpaRepositories. <jpa:auditing /> Enables transparent auditing of JPA managed entities. Note that this requires the AuditingEntityListener applied to the entity (either globally through a declaration in orm.xml or through @EntityListener on the entity class). MongoDB For Spring Data MongoDB XML namespace elements not mentioning a GHGLFDWHG#(QDEOHDQQRWDWLRQDOWHUQDWLYH\RXXVXDOO\GHFODUHDQ#%HDQ annotated method and use the plain Java APIs of the classes that would have otherwise been set up by the XML element. Alternatively, you can use WKH-DYD&RQͤJEDVHFODVV$EVWUDFW0RQJR&RQͤJXUDWLRQWKDW6SULQJ'DWD MongoDB ships for convenience. XML element Description <mongo:db-factory /> One stop shop to set up a Mongo instance pointing to a particular database instance. For advanced-use cases define a <mongo:mongo /> extenally and refer to it using a mongo-ref attribute. <mongo:mongo /> Configures a Mongo instance. Supports basic attributes like host, port, write concern etc. Configure more advanced options through the nested <mongo:options /> element. In JavaConfig simply declare an @Bean method returning a Mongo instance. <mongo:mapping-converter /> Configures a MappingMongoConverter. CONTENTS INCLUDE: kAbout the Spring Data Project kConfiguration Support kObject Mapping kTemplate APIs kRepositories kAdvanced Features... and more! Core Spring Data
  53. Upcoming Webinar - Couchbase & Spring Data
 http:/ /bit.ly/couchbase-and-spring-data Codd

    GA release
 http:/ /bit.ly/sd-codd !53
  54. More information Oliver Gierke - ogierke@gopivotal.com Thomas Darimont - tdarimont@gopivotal.com

    http:/ /projects.spring.io/spring-data http:/ /bit.ly/stack-overflow-spring-data !54