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

Whats new in Spring Data?

Whats new in Spring Data?

This talk will give a broad overview of the new features introduced in the latest Spring Data release trains. We will cover recent additions and improvements in Spring Data Commons - the module that's shared amongst the store specific ones.

We'll then delve into the latest and greatest features of individual store modules, like JPA, MongoDB, Neo4j, Solr and the community ones as well.

Thomas Darimont

November 06, 2014
Tweet

More Decks by Thomas Darimont

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. Spring Data Modules Commons Neo4j Gemfire JPA Solr ElasticSearch REST

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

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

    Solr ElasticSearch REST Cassandra Couchbase Redis MongoDB Community
 modules Core
 modules
  6. 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
  7. Custom Repository Implementations interface FooRepoCustom - List<Foo> someJdbcBatchOperation(…) class FooRepoImpl

    extends JdbcDaoSupport implements FooRepoCustom interface FooRepo extends CrudRepository<User, Id>, FooRepoCustom
  8. Page example interface UserRepository extends Repository<User, Long> { //findByLastname("Matthews", new

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

    PageRequest(1, 5, ASC, "firstName")) Slice<User> findSliceByLastname(String lastname, Pageable pageable); } • 1 query • but for 5 + 1 records
  10. default method example ! ! interface CustomerRepository extends Repository<Customer, Long>

    { ! Optional<Customer> findByLastname(String lastname); ! default Optional<Customer> findByLastname(Customer cust) { return findByLastname(cust == null ? null : cust.lastname); } } Lean way to add custom functionality to a Repository
  11. Future<T> example interface UserRepository extends Repository<User, Long> { //findByLastname("Darimont").get(5, SECONDS);

    @Async Future<Person> findByLastname(String lastname); } Uses async task execution infrastructure —> @EnableAsync
  12. Caching example @CacheConfig("users") interface CachingUserRepository extends Repository<User, Long> { @CachePut(key="#user.username")

    <S extends User> S save(S user); ! @Cacheable User findByUsername(String username); } ! ! Uses Spring’s caching infrastructure —> @EnableCaching
  13. deleteBy… example interface UserRepository extends Repository<User, Long> { List<User> deleteByLastname(String

    lastname); } • Deleted items are returned • Considers @PreRemove, @PostRemove
  14. 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
  15. Limiting result sets • Previously Pageable/Page to dynamically restrict results

    • first / top equal stylistic variants interface StudentRepository extends Repository<Student, Long> { List<Student> findTop10ByNameLikeOrderByAgeAsc(String name); ! //findFirst5By(new Sort(DESC, “score")); //top 5 students List<Student> findFirst5By(Sort sort); }
  16. Sort by QueryDSL type-safe order criteria use QSort if you

    need sort only QUser user = QUser.user; ! List<User> users = repository.findAll( user.lastname.eq("Borat") , user.dateOfBirth.yearMonth().asc() );
  17. 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))
  18. 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)
  19. 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
  20. 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
  21. Spring Security integration • Expose current Spring Security context in

    @Query • Gives access to the SpringSecurityExpressionRoot • SPI to extend the SpEL context for Repositories
  22. 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
  23. Redis Improvements • Upgrade to Spring Framework 4.0 • SCAN

    support • Spring Boot integration • Sentinel support
  24. SCAN Support • Non-blocking alternative to • KEYS • SMEMBERS

    ScanOptions options = ScanOptions.scanOptions().count(99).build(); Cursor<String> cursor = setOperations.scan(key, options); ! while(cursor.hasNext()){ process(cursor.next()); }
  25. Sentinel support Redis Master Redis Slave1 Redis Slave2 Sentinel Node1

    Sentinel Node2 Sentinel Node3 Redis Master Redis Master* Replication
  26. REST Improvements • Upgrade to Spring Framework 4.0 • ALPS

    support • Enhanced Projections • JSON Patch
  27. 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
  28. 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