Slide 1

Slide 1 text

Unless otherwise indicated, these slides are © 2013-2014 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ What’s new in Spring Data? Thomas Darimont, Software Engineer, Pivotal Inc. @thomasdarimont

Slide 2

Slide 2 text

Agenda Overview Repositories Store modules Q&A

Slide 3

Slide 3 text

Versions?

Slide 4

Slide 4 text

Release train

Slide 5

Slide 5 text

Release train • Similar to the eclipse release model • Modules still versioned independently • Single canonical name —> Evans - SR1 • Communicate compatibility between modules • Helps to avoid confusion with versions • Now used by Spring Boot

Slide 6

Slide 6 text

Spring Data Release train BOM org.springframework.data spring-data-releasetrain Evans-SR1 import pom

Slide 7

Slide 7 text

Release train 03/2013 Arora 09/2013 Babbage 02/2014 Codd 05/2014 Dijkstra 09/2014 Evans 11/2014 Fowler*

Slide 8

Slide 8 text

Spring Data Modules Commons Neo4j Gemfire JPA Solr ElasticSearch REST Cassandra Couchbase Redis MongoDB Community
 modules Core
 modules

Slide 9

Slide 9 text

Module setup - Codd Commons Neo4j Gemfire JPA Solr ElasticSearch REST Cassandra Couchbase Redis MongoDB Community
 modules Core
 modules

Slide 10

Slide 10 text

Module setup - Dijkstra / Evans Commons Neo4j Gemfire JPA Solr ElasticSearch REST Cassandra Couchbase Redis MongoDB Community
 modules Core
 modules

Slide 11

Slide 11 text

Evans SR1 Released in Oct 2014

Slide 12

Slide 12 text

Spring Data Release Train Evans - Major Themes • Upgrade to Spring Framework 4.0 • Support for Java 8 • Additional keywords to statically restrict results • Enhanced support for Spring Security • Improved multi-store configuration • MongoDB 2.6 features • Redis Sentinel • ALPS and except Projections for Spring Data REST

Slide 13

Slide 13 text

Spring Data
 Repositories Pragmatic data access APIs

Slide 14

Slide 14 text

Quick refresher

Slide 15

Slide 15 text

Spring Data
 Repositories Pragmatic data access APIs

Slide 16

Slide 16 text

Interface based programming model No custom implementations required, but possible

Slide 17

Slide 17 text

Base Abstractions Repository, CrudRepository, PagingAndSortingRepository Store specific variants

Slide 18

Slide 18 text

Query derivation List findByFirstnameAndLastname(String first, String last) ! select u from User u where u.firstname = ? and u.lastname = ?

Slide 19

Slide 19 text

defined Queries @Query Annotation JPA named queries Spring Data named queries

Slide 20

Slide 20 text

Flexible predicates QueryDSL - type safe queries for Java QuerydslPredicateExecutor

Slide 21

Slide 21 text

Custom Repository Implementations interface FooRepoCustom - List someJdbcBatchOperation(…) class FooRepoImpl extends JdbcDaoSupport implements FooRepoCustom interface FooRepo extends CrudRepository, FooRepoCustom

Slide 22

Slide 22 text

New & Noteworthy

Slide 23

Slide 23 text

Query methods

Slide 24

Slide 24 text

Slices Paging without the overhead

Slide 25

Slide 25 text

Page example interface UserRepository extends Repository { //findByLastname("Matthews", new PageRequest(1, 5, ASC, "firstName")) Page findByLastname(String lastname, Pageable pageable); } • 1 query for count • 1 query for page data

Slide 26

Slide 26 text

Slices example interface UserRepository extends Repository { //findSliceByLastname("Matthews", new PageRequest(1, 5, ASC, "firstName")) Slice findSliceByLastname(String lastname, Pageable pageable); } • 1 query • but for 5 + 1 records

Slide 27

Slide 27 text

Java 8 support Optional and default methods

Slide 28

Slide 28 text

Optional example interface PersonRepository extends Repository { //findByLastname("Darimont").orElse(johnDoe); Optional findByLastname(String lastname); } No more NullPointerExceptions with Java 8 or Guava

Slide 29

Slide 29 text

default method example ! ! interface CustomerRepository extends Repository { ! Optional findByLastname(String lastname); ! default Optional findByLastname(Customer cust) { return findByLastname(cust == null ? null : cust.lastname); } } Lean way to add custom functionality to a Repository

Slide 30

Slide 30 text

Future support Async repository method executions

Slide 31

Slide 31 text

Future example interface UserRepository extends Repository { //findByLastname("Darimont").get(5, SECONDS); @Async Future findByLastname(String lastname); } Uses async task execution infrastructure —> @EnableAsync

Slide 32

Slide 32 text

Caching support

Slide 33

Slide 33 text

Caching example @CacheConfig("users") interface CachingUserRepository extends Repository { @CachePut(key="#user.username") S save(S user); ! @Cacheable User findByUsername(String username); } ! ! Uses Spring’s caching infrastructure —> @EnableCaching

Slide 34

Slide 34 text

deleteBy… support Query derivation for delete methods

Slide 35

Slide 35 text

deleteBy… example interface UserRepository extends Repository { List deleteByLastname(String lastname); } • Deleted items are returned • Considers @PreRemove, @PostRemove

Slide 36

Slide 36 text

SpEL in @Query Generic queryMethods

Slide 37

Slide 37 text

SpEL in @Query ! @Modifying @Query("update #{#entityName} u set u.active= :activeState”) void updateActiveState(@Param("activeState") boolean state); • Define generic queries in common repository interfaces • In Spring Data JPA - support for other stores follows

Slide 38

Slide 38 text

Limiting result sets You’re only interested in the top ones anyway, right?

Slide 39

Slide 39 text

Limiting result sets • Previously Pageable/Page to dynamically restrict results • first / top equal stylistic variants interface StudentRepository extends Repository { List findTop10ByNameLikeOrderByAgeAsc(String name); ! //findFirst5By(new Sort(DESC, “score")); //top 5 students List findFirst5By(Sort sort); }

Slide 40

Slide 40 text

Sort by QueryDSL Define your sort criteria via QueryDSL expressions

Slide 41

Slide 41 text

Sort by QueryDSL type-safe order criteria use QSort if you need sort only QUser user = QUser.user; ! List users = repository.findAll( user.lastname.eq("Borat") , user.dateOfBirth.yearMonth().asc() );

Slide 42

Slide 42 text

Miscellaneous

Slide 43

Slide 43 text

CDI improvements custom implementations and eager instantiation

Slide 44

Slide 44 text

Common geo-spatial types

Slide 45

Slide 45 text

Null-Handling in Sort Support for nulls first - nulls last

Slide 46

Slide 46 text

Null handling in sort new Sort(new Order(“foo").nullsFirst()) new Sort(new Order(“foo”).nullsLast()) new Sort(new Order(“foo”).nullsNative()) new Sort(new Order(“foo”).with(NullHandling.NULLS_FIRST))

Slide 47

Slide 47 text

Multi-store
 configuration We know what you were thinking!

Slide 48

Slide 48 text

Improved multi-store configuration ! • Triggered if multiple Spring Data modules are detected • Modules only register for interfaces that a assigned to the store • Annotations on domain types (@Entity, @Document) • Store specific repository base interface (not recommended)

Slide 49

Slide 49 text

Debug output … DEBUG … - Multiple Spring Data modules found, entering strict repository configuration mode! ! … DEBUG … - Spring Data JPA - Could not safely identify store assignment for repository candidate interface ….OrderRepository. ! … DEBUG … - Spring Data JPA - Registering repository:
 Interface: ….CustomerRepository Factory: ….JpaRepositoryFactoryBean

Slide 50

Slide 50 text

Hidden Gems Some helpful constructs

Slide 51

Slide 51 text

Repositories Collection of all discovered Repositories

Slide 52

Slide 52 text

RepositoryPopulator Preload your Repositories at start-up

Slide 53

Slide 53 text

Store module
 improvements

Slide 54

Slide 54 text

JPA Java Persistence API 2.1 support

Slide 55

Slide 55 text

JPA Improvements • Upgrade to Spring Framework 4.0 • JPA 2.1 support • Execution of stored procedures • Support for entity graph definitions • Integration with Spring Security • Support for deleteBy queries • Support for findTopK / findFirstK

Slide 56

Slide 56 text

Spring Security integration • Expose current Spring Security context in @Query • Gives access to the SpringSecurityExpressionRoot • SPI to extend the SpEL context for Repositories

Slide 57

Slide 57 text

MongoDB

Slide 58

Slide 58 text

MongoDB Improvements • Upgrade to Spring Framework 4.0 • Support for Slices • Support for deleteBy queries • Lazy-loading for @DBref’s • Aggregation Framework enhancements • Query modifiers via @Meta • Support for MongoDB 2.6 • Support for TextSearch

Slide 59

Slide 59 text

Neo4j

Slide 60

Slide 60 text

Neo4j Improvements • Upgrade to Spring Framework 4.0 • Support for Neo4j 2.0 • Support for Slices

Slide 61

Slide 61 text

Redis

Slide 62

Slide 62 text

Redis Improvements • Upgrade to Spring Framework 4.0 • SCAN support • Spring Boot integration • Sentinel support

Slide 63

Slide 63 text

SCAN Support • Non-blocking alternative to • KEYS • SMEMBERS ScanOptions options = ScanOptions.scanOptions().count(99).build(); Cursor cursor = setOperations.scan(key, options); ! while(cursor.hasNext()){ process(cursor.next()); }

Slide 64

Slide 64 text

Sentinel support Redis Master Redis Slave1 Redis Slave2 Sentinel Node1 Sentinel Node2 Sentinel Node3 Redis Master Redis Master* Replication

Slide 65

Slide 65 text

REST

Slide 66

Slide 66 text

REST Improvements • Upgrade to Spring Framework 4.0 • ALPS support • Enhanced Projections • JSON Patch

Slide 67

Slide 67 text

ALPS support and excerpt projections • Application Level Profile Semantics • Spring Data REST exposes resources describing the service • Interface based programming model to define custom projections • Configuration to define except projections

Slide 68

Slide 68 text

Community Modules

Slide 69

Slide 69 text

Cassandra

Slide 70

Slide 70 text

Couchbase

Slide 71

Slide 71 text

ElasticSearch

Slide 72

Slide 72 text

What’s next?

Slide 73

Slide 73 text

InMemoryRepository

Slide 74

Slide 74 text

Spring Security++

Slide 75

Slide 75 text

Reactive Repositories

Slide 76

Slide 76 text

Spring Data Examples https://github.com/spring-projects/spring-data-examples

Slide 77

Slide 77 text

Related talks

Slide 78

Slide 78 text

Spring Data Track ! • 12:30 Caching with Spring - Michael Plöd • 14:30 Boot your search with Spring - Christoph Strobl, Artur Konczak • 15:45 Spring Data REST: Repositories meet Hypermedia - Oliver Gierke

Slide 79

Slide 79 text

Q & A