Save 37% off PRO during our Black Friday Sale! »

Spring Data Repositories - A Deep Dive

Spring Data Repositories - A Deep Dive

Slides of my talk at SpringOne 2013, Santa Clara, CA.

977c74bb044a9d4fa90b305824eda390?s=128

Oliver Drotbohm

September 11, 2013
Tweet

Transcript

  1. © 2013 SpringOne 2GX. All rights reserved. Do not distribute

    without permission. Spring Data Repositories Best Practices Oliver Gierke & Thomas Darimont
  2. 2 Oliver Gierke Spring Data Engineer Project lead Core/JPA/MongoDB ogierke@gopivotal.com

    www.olivergierke.de olivergierke
  3. 3 Thomas Darimont Spring Data Engineer tdarimont@gopivotal.com thomasdarimont www.tutorials.de Core/JPA/MongoDB

  4. 4 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 1 free copy per attendee! Gemfire Splunk
  5. 5 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 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 CONTENTS INCLUDE: kAbout the Spring Data Project kConfiguration Support kObject Mapping kTemplate APIs kRepositories kAdvanced Features... and more! Core Spring Data
  6. 6 Hands on

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

  8. 8 Step 0 Initial project setup

  9. 9 " How to get a Spring Data JPA based

    project up and running quickly?
  10. 10 Summary Check out Spring Boot Easily configure dependencies for

    your project Defaults application config based on classpath
  11. 11 Step 1 Basic JPA infrastructure setup

  12. 12 " Persistence technology of choice is JPA. e application

    uses JavaConfig and sample data contained in data.sql.
  13. 13 Summary Easy setup through JavaConfig XML-less JPA configuration

  14. 14 Step 2 Quickstart

  15. 15 " 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.
  16. 16 Summary Interface-based programming model No implementation required Queries derived

    from method names
  17. 17 Step 3 Extended CRUD methods

  18. 18 " Customers can be deleted and obtained all at

    once.
  19. 19 Summary Switched to CrudRepository Exposed CRUD methods Broad API

    exposed
  20. 20 Step 4 Pagination

  21. 21 " Customers can be accessed page by page.

  22. 22 Summary Switched to PagingAndSortingRepository Exposed CRUD methods and paging

    ones Broad API exposed
  23. 23 Step 5 Re-declaring existing CRUD methods

  24. 24 " CustomerRepository.findAll() should rather return a List. e transaction

    timeout for save(…) should be customized to 10 seconds.
  25. 25 Summary Re-declare methods to customize Return types Annotation config

    (Tx, Locking, Query, Hints)
  26. 26 Step 6 Introducing a read-only repository base interface

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

  28. 28 Summary Craft custom base interface Return types Narrow down

    the API to the necessary parts
  29. 29 Step 7 Using manually defined queries

  30. 30 " As a user, I want to look up

    products by their custom attributes.
  31. 31 Summary @Query annotation JPA named queries Spring Data named

    queries
  32. 32 Step 8 Flexible predicate execution

  33. 33 " As a user, I want to search for

    customers by first name, last name, email address and any combination of them
  34. 34 Summary Querydsl - type safe queries for Java QuerydslPredicateExecutor

  35. Side track: Repository proxies 35

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

    Spring FactroyBean / CDI XML namespace / @EnableRepositories 36
  37. Proxy mechanism Query interface CRUD implementation class QueryDslPredicateExecutor Custom implementation

    extension 37
  38. 38 Step 9 Custom implementations for repositories

  39. 39 " As an admin user, I'd like to use

    custom code to delete all products beyond a given price.
  40. 40 Summary Provide custom implementation Base class support (Querydsl)

  41. Stuff on top Spring MVC integration Spring Data REST Spring

    Boot 41
  42. Up and beyond 42

  43. 43 Supported modules JPA MongoDB Neo4j Gemfire

  44. 44 Community modules Solr Elasticsearch Couchbase FuzzyDB (Cassandra)

  45. 45 Further sessions Tackling Big Data Complexity with Spring -

    Wed, 2:30pm Your Data, Your Search, Elasticsearch - Wed, 2:30pm Spring Data Community Lightning Talks - Thu, 8:30am Researching Cancer in the Cloud with Spring - Thu, 12:45pm
  46. Summary 46

  47. Interface-based programming model 47

  48. Start simple, get more sophisticated 48

  49. Declarative query execution 49

  50. Flexible predicate execution 50

  51. Custom implementation 51

  52. CDI integration 52