Spring Data Repositories - Best Practices
description
Transcript of Spring Data Repositories - Best Practices
© 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.
Spring Data RepositoriesBest PracticesOliver Gierke & Thomas Darimont
Wednesday, September 11, 13
2
Oliver Gierke
Spring Data EngineerProject lead Core/JPA/MongoDB
Wednesday, September 11, 13
3
Thomas Darimont
Spring Data Engineer
thomasdarimontwww.tutorials.de
Core/JPA/MongoDB
Wednesday, September 11, 13
4
Spring DataModern Data Access For Enterprise Java
NoSQLJPA
JDBC
Redis
Big Data Hadoop
HBase
MongoDB Neo4j REST exporter
Roo
HivePig
Querydsl Repositories
1 free copy per attendee!
Gemfire
Splunk
Wednesday, September 11, 13
5
Get
Mor
e R
efca
rdz!
Vis
it r
efca
rdz.
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 VWRUH�VSHFLͤF�IHDWXUHV�DQG�FDSDELOLWLHV�
General Themes,QIUDVWUXFWXUH�&RQͤJXUDWLRQ�6XSSRUW$�FRUH�WKHPH�RI�DOO�WKH�6SULQJ�'DWD�SURMHFWV�LV�VXSSRUW�IRU�FRQͤJXULQJ�resources to access the underlying technology. This support is implemented using XML namespaces and support classes for Spring -DYD&RQͤJ�DOORZLQJ�\RX�WR�HDVLO\�VHW�XS�DFFHVV�WR�D�0RQJR�GDWDEDVH��DQ�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.
2EMHFW�0DSSLQJ�)UDPHZRUNMost of the NoSQL Java APIs do not provide support to map domain
JPAXML 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).
MongoDBFor Spring Data MongoDB XML namespace elements not mentioning a GHGLFDWHG�#(QDEOH�DQQRWDWLRQ�DOWHUQDWLYH��\RX�XVXDOO\�GHFODUH�DQ�#%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ͤJ�EDVH�FODVV�$EVWUDFW0RQJR&RQͤJXUDWLRQ�WKDW�6SULQJ�'DWD�MongoDB ships for convenience.
XML element Description<mongo:db-factory /> One stop shop to set up a Mongo
CONTENTS INCLUDE:
k�About the Spring Data Projectk�Configuration Supportk�Object Mappingk�Template APIsk�Repositoriesk�Advanced Features... and more!
Core Spring Data
Wednesday, September 11, 13
6
Hands on
Wednesday, September 11, 13
Sample codehttps://github.com/olivergierke/repositories-deepdive
7Wednesday, September 11, 13
8
Step 0Initial project setup
Wednesday, September 11, 13
9
" How to get a Spring Data JPA based project up and running quickly?
Wednesday, September 11, 13
10
SummaryCheck out Spring Boot
Easily configure dependencies for your project
Defaults application config based on classpath
Wednesday, September 11, 13
11
Step 1Basic JPA infrastructure setup
Wednesday, September 11, 13
12
" Persistence technology of choice is JPA. e application uses JavaConfig and sample data contained in data.sql.
Wednesday, September 11, 13
13
SummaryEasy setup through JavaConfig
XML-less JPA configuration
Wednesday, September 11, 13
14
Step 2Quickstart
Wednesday, September 11, 13
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.
Wednesday, September 11, 13
16
SummaryInterface-based programming model
No implementation required
Queries derived from method names
Wednesday, September 11, 13
17
Step 3Extended CRUD methods
Wednesday, September 11, 13
18
" Customers can be deleted and obtained all at once.
Wednesday, September 11, 13
19
SummarySwitched to CrudRepository
Exposed CRUD methods
Broad API exposed
Wednesday, September 11, 13
20
Step 4Pagination
Wednesday, September 11, 13
21
" Customers can be accessed page by page.
Wednesday, September 11, 13
22
SummarySwitched to PagingAndSortingRepository
Exposed CRUD methods and paging ones
Broad API exposed
Wednesday, September 11, 13
23
Step 5Re-declaring existing CRUD methods
Wednesday, September 11, 13
24
" CustomerRepository.findAll() should rather return a List. e transaction timeout for save(…) should be customized to 10 seconds.
Wednesday, September 11, 13
25
SummaryRe-declare methods to customize
Return types
Annotation config (Tx, Locking, Query, Hints)
Wednesday, September 11, 13
26
Step 6Introducing a read-only repository base interface
Wednesday, September 11, 13
27
" Products shall be accessible in read-only mode only.
Wednesday, September 11, 13
28
SummaryCraft custom base interface
Return types
Narrow down the API to the necessary parts
Wednesday, September 11, 13
29
Step 7Using manually defined queries
Wednesday, September 11, 13
30
" As a user, I want to look up products by their custom attributes.
Wednesday, September 11, 13
31
Summary@Query annotation
JPA named queries
Spring Data named queries
Wednesday, September 11, 13
32
Step 8Flexible predicate execution
Wednesday, September 11, 13
33
" As a user, I want to search for customers by first name, last name, email address and any combination of them
Wednesday, September 11, 13
34
SummaryQuerydsl - type safe queries for Java
QuerydslPredicateExecutor
Wednesday, September 11, 13
Side track:Repository proxies
35Wednesday, September 11, 13
Proxy mechanismUsing Spring's JDK proxy support
Bootstrap through Factory
Spring FactroyBean / CDI
XML namespace / @EnableRepositories
36Wednesday, September 11, 13
Proxy mechanismQuery interface
CRUD implementation class
QueryDslPredicateExecutor
Custom implementation extension
37Wednesday, September 11, 13
38
Step 9Custom implementations for repositories
Wednesday, September 11, 13
39
" As an admin user, I'd like to use custom code to delete all products beyond a given price.
Wednesday, September 11, 13
40
SummaryProvide custom implementation
Base class support (Querydsl)
Wednesday, September 11, 13
Stuff on topSpring MVC integration
Spring Data REST
Spring Boot
41Wednesday, September 11, 13
Up and beyond
42Wednesday, September 11, 13
43
Supported modulesJPA
MongoDB
Neo4j
Gemfire
Wednesday, September 11, 13
44
Community modulesSolr
Elasticsearch
Couchbase
FuzzyDB
(Cassandra)
Wednesday, September 11, 13
45
Further sessionsTackling 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
Wednesday, September 11, 13
Summary
46Wednesday, September 11, 13
Interface-based programming model
47Wednesday, September 11, 13
Start simple, getmore sophisticated
48Wednesday, September 11, 13
Declarativequery execution
49Wednesday, September 11, 13
Flexible predicate execution
50Wednesday, September 11, 13
Customimplementation
51Wednesday, September 11, 13
CDI integration
52Wednesday, September 11, 13