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

What's new in Spring Data - Fowler?

What's new in Spring Data - Fowler?

B0e9230af37f3a883c88d3f11c00e174?s=128

Christoph Strobl

April 22, 2015
Tweet

Transcript

  1. Christoph Strobl | Pivotal Software, Inc. What’s new in Spring

    Data Fowler? @stroblchristoph
  2. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Agenda 2 Overview Core Themes Store Specifics Outlook / Q&A
  3. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Overview 3
  4. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 11+ Modules 4
  5. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Modules 5 Commons Neo4j Gemfire JPA Solr ElasticSearch REST Cassandra Couchbase Redis MongoDB Community
 modules Core
 modules Aerospike Hazelcast Crate Incubating
  6. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Release train 6 03/2015 Fowler 03/2013 Arora 09/2013 Babbage 02/2014 Codd 05/2014 Dijkstra 09/2014 Evans 09/2015 Gosling* image source: wikipedia CC:BY
  7. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Release train BOM <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-releasetrain</artifactId> <version>Fowler-RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> 7 <dependency> <groupId>org.springframework.data</groupI <artifactId>spring-data-rest-webmvc</arti </dependency> <dependency> <groupId>org.springframework.data</groupI <artifactId>spring-data-mongodb</artifactI <dependencyManagement>
 <dependencies>
  8. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Working with Spring Data 8
  9. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ The Goal… “…provide a familiar and consistent Spring based programming model retaining store specific features and capabilities” (O.Gierke) 9
  10. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Interface based programming model 10 interface PersonRepository extends Repository<Person, Long> {} interface PersonRepository extends CrudRepository<Person, Long> {} interface PersonRepository extends PagingAndSortingRepository<Person, Long> {}
  11. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Query Derivation 11 interface PersonRepository extends CrudRepository<Person, Long> { ! ! ! ! } Page<Person> findByFirstname(String firstname, Pageable page); List<Person> findByFirstnameAndLastname(String firstname, String lastname); @Query("select u from User u where u.firstname = ?0 and u.lastname = ?1")
  12. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ QueryDSL Integration* 12 
 ! ! ! } Page<Person> findByFirstname(String firstname, Pageable page); List<Person> findByFirstnameAndLastname(String firstname, String lastname); , QueryDslPredicateExecutor { { interface PersonRepository extends CrudRepository<Person, Long> @Query("select u from User u where u.firstname = ?0 and u.lastname = ?1")
  13. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Custom Implementations 13 interface PersonRepository extends CrudRepository<Person, Long> { ! ! ! ! } Page<Person> findByFirstname(String firstname, Pageable page); List<Person> findByFirstnameAndLastname(String firstname, String lastname); @Query("select u from Person p where p.firstname = ?0 and p.lastname = ?1") interface PersonRepositoryCustom { void doSomething(Person person) } 1 class PersonRepositoryImpl implements PersonRepositoryCustom { void doSomething(Person person) { ... } 2 ,PersonRepositoryCustom { 3
  14. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 14 GemFire 8 Performance REST Projections JSR-310 Improvements MongoDB Script Execution GeoJson Support Redis HyperLogLog Solr Stats Solr Realtime Get JPA Access Type Map Backed Interfaces CDI Support for SD Cassandra GemFire AsyncEventQueues RedisCache improvements @Version support for REST Spring Data Fowler Java 8
  15. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Java 8 Steams 15 interface PersonRepository extends CrudRepository<Person, Long> { ! ! ! ! } Stream<Person> findPersonBy(); …for JPA and MongoDB @Query("select p from Person p") Stream<Person> streamAllPersons(); Always make sure to close the Stream. try (Stream<Customer> stream = repo.findPersonBy()) {
 //...
  16. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Slice and Page support map 16 Page<String> page = .... Page<Integer> converted = page.map(new Converter<String, Integer>() { @Override public Integer convert(String source) { return source.length(); } }); …result transformation made easy. Page<Integer> converted = page.map(String::length); Converter is a
 @FunctionalInterface
  17. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ JSR-310 and the ThreeTen back port 17 …aka Java 8 DateTime org.threeten.bp.Instant; org.threeten.bp.LocalDate; org.threeten.bp.LocalDateTime; org.threeten.bp.LocalTime; org.threeten:threetenbp java.time.Instant; java.time.LocalDate; java.time.LocalDateTime; java.time.LocalTime; Java 8 @EntityScan(
 basePackageClasses = { Application.class, Jsr310JpaConverters.class } ) @SpringBootApplication class Application { … }
  18. interface PersonRepository extends …, QueryDslPredicateExecutor { ! ! ! }

    Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software, Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ QueryDslPredicateExecutor Extensions 18 …exists and findAll using Predicates Iterable<T> findAll(Predicate predicate, OrderSpecifier<?>... orders); boolean exists(Predicate predicate); repository.findAll(person.dateOfBirth.after(today()), person.firstname.asc()); repository.exists(person.firstname.eq("Eddard"));
  19. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Performance tweaks 19 …getting the most out of it. ops/sec 0 600.000 1.200.000 1.800.000 2.400.000 Read
 Write
 Evans Fowler higher is better
  20. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ And more…. § Add support for NotContaining keyword § Performance enhancements for RepositoryInterfaceAwareBeanPostProcessor. § DefaultCrudMethods now exposes accessible methods. § Introduced a Range value type § Allow transaction behavior to be configurable. 20
  21. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Store Specifics
 Fowler 21
  22. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data JPA 22
  23. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ JPA Fetch Graphs 23 interface PersonRepository extends CrudRepository<Person, Long> { ! ! ! ! ! } …also for base repository methods @EntityGraph(type = EntityGraphType.LOAD, value = "Person.overview") List<Person> findAll(); ! Person findOne(Long id); @EntityGraph(type = EntityGraphType.LOAD, value = "Person.address") @EntityGraph(attributePaths = {"roles", "colleagues.roles"}) List<Person> findAll(); More to come for Gosling
  24. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ SpEL Enhancements 24 interface PersonRepository extends CrudRepository<Person, Long> { ! ! ! ! ! ! ! ! ! ! ! ! } …everything at your fingertips. @Query("..where u.email like ?#{hasRole('ROLE_ADMIN') ? '%' : principal.em...") List<BusinessObject> findBusinessObjectsForCurrentUser(); @Query("select p from #{#entityName} p where p.first = ?#{[0]} and p.last = ?#{[1]}") List<Person> findUsersWithEntityExpression(String first, String last); @Query( value = "select * from (select rownum() as RN, p.* from Person p) where RN between ?#{ #pageable.offset -1} and ?#{#pageable.offset + #pageable.pageSize}", countQuery = "select count(p.id) from Person p", nativeQuery = true) Page<Person> findAllWithNativePagination(Pageable pageable);
  25. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ And more…. § Avoid query creation in PagedExecution if count query returns 0. § javax.persistence.Version (honored) § Allow composite keys to be used in SimpleJpaRepository.findAll(Iterable<ID>). § Added support for REF_CURSOR output parameters for stored procedures. 25
  26. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data MongoDB 26
  27. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ MongoDB 3.0 27 …driver and database compatibility 2.4 2.6 3.0 MongoDB Server 2.11 2.12 2.13 3.0 MongoDB Java Driver compatibility matrix source: mongodb.org
  28. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ MongoDB 3.0 28 …driver and database compatibility D e p r e cat e d <mongo:mongo host="127.0.0.1" port="27017"> <mongo:options connections-per-host="${mongo.connectionsPerHost}" <mongo:mongo-client host="127.0.0.1" port="27017"> <mongo:client-options write-concern="NORMAL" The upcoming Gosling release will provide more sophisticated support for MongoDB 3.0 Features.
  29. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Geospatial enhancements 29 dedicated types for GeoJSON. interface StoreRepository extends CrudRepository<Store, Long> { ! ! } List<Store> findByLocationWithin(Polygon polygon); class Store { ! String id; @GeospatialIndexed Point location; } location :[ x, y ] legacy coordinate format
  30. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Geospatial enhancements 30 dedicated types for GeoJSON. interface StoreRepository extends CrudRepository<Store, Long> { ! ! } List<Store> findByLocationWithin(Polygon polygon); repo.findByLocationWithin( new Polygon( new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348), new Point(-73.991658, 40.730006))); { "location": { "$geoWithin": { "$polygon": [ [-73.992514,40.758934], [-73.961138,40.760348],
 ...
  31. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Geospatial enhancements 31 dedicated types for GeoJSON. interface StoreRepository extends CrudRepository<Store, Long> { ! ! } List<Store> findByLocationWithin(Polygon polygon); class Store { ! String id; GeoJsonPoint location; } location : { "type" : "Point", "coordinates" : [ x, y ] } spherical
  32. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Geospatial enhancements 32 dedicated types for GeoJSON. interface StoreRepository extends CrudRepository<Store, Long> { ! ! } List<Store> findByLocationWithin(Polygon polygon); repo.findByLocationWithin( new GeoJsonPolygon( new Point(-73.992514, 40.758934), new Point(-73.961138, 40.760348), new Point(-73.991658, 40.730006), new Point(-73.992514, 40.758934))); { "location": { "$geoWithin": { "$geometry": { "type": "Polygon", "coordinates": [ [ [-73.992514,40.758934], [-73.961138,40.760348],
 ...
  33. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Serverside Script execution 33 …run JavaScript on MongoDB ! 
 ExecutableMongoScript script = new ExecutableMongoScript(scriptString); operations.scriptOps().execute(script, "jax2015"); operations.scriptOps().register(new NamedMongoScript("echoScript", script)); operations.scriptOps().call("echoScript", "The Java Conference"); String scriptString = "function(x) { return x; }";
  34. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ And more…. § Explicitly annotated Field should not be considered Id. § $mul, $bit, $position for Update § $minDistance for geoNear § Containing now searches inside strings and collections. § Add support for fast insertion via MongoRepository.insert(... 34
  35. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Redis 35
  36. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ HyperLogLog Support 36 …or count distinct. hllOps.add("hll2", "x", "y", "z"); hllOps.union("hll3", "hll", "hll2"); redis> PFADD hll a b c d e f g (integer) 1 redis> PFCOUNT hll (integer) 7 redis> pfadd hll2 x y z (integer) 1 redis> pfmerge hll3 hll hll2 (integer) 1 redis> pfcount hll3 (integer) 10 hllOps = redisTemplate.opsForHyperLogLog(); hllOps.add("hll", "a", "b", "c",... hllOps.size("hll");
  37. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Gemfire 37
  38. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Gemfire 8 38 Cluster setup revised. <gfe:cache use-cluster-configuration="true" • Read Cluster Configuration from Network Locator • Keeps your members sane when joining the cluster
  39. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Rest 39
  40. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ @Version based ETags 40 …as simple as it should be. class Customer { ! @Version Long version; @LastModifiedDate LocalDate lastModifiedDate; } curl -v http://…/customers/1 ! Etag: 1 Last-Modified: Tue, 24 Mar 2015 12:34:56 GMT
  41. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ JSON Schema 41 curl http://…/alps/stores ! { "version": "1.0", "descriptors": [ { "id": "store-representation", "href": "…/stores/schema", "descriptors": [ … ] }], … } { "title": "example.stores.Store", "properties": { "address": { "$ref": "#/descriptors/address" }, "name": { "type": "string" } }, "descriptors": { "address": { "type": "object", "point": { "type": "object",
  42. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Solr 42
  43. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Realtime Get 43 // realtime-get fetches uncommitted document template.getById(document.getId(), Document.class); …read uncommitted changes. // add document but delay commit for 3 seconds template.saveBean(document, 3000); // document will not be returned hence not yet committed to the index template.queryForObject(query, Document.class);
  44. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ @Score to retrieve document relevance 44 @SolrDocument
 class Product { @Id String id; String name;
 @Indexed(readOnly=true) Float score; } @SolrDocument
 interface ProductRepository extends CrudRepository { @Query(fields={"*", "score"} List<Product> findByName(String name); } @Score Float score;
  45. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Where’s the code? 45
  46. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Oh, wait… There’s more - sneak peak on what’s to come! 46
  47. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Key/Value 47 …for any kind of identifiable pairs. interface KeyValueAdapter { put(id, value, keyspace) get(id, keyspace) delete(id, keyspace) } class KeyValueTemplate { } class QueryEngine { execute(query, keyspace) } interface KeyValueRepository { … } MapKeyValueAdapter
 available by default SpELQueryEngine
 use with compiled SpEL -D spring.expression.compiler.mode KeyValueTemplate(keyValueAdapter)
  48. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Spring Data Key/Value 48 …for any kind of identifiable pairs. interface PersonRepository extends CrudRepository<Person, Long> { ! } List<Person> findByAgeGreaterThan(int age); @Configuration @EnableMapRepositories class Config {} @Autowired PersonRepository repository;
 
 Person person = repository.save(new Person("Dave", "Matthews", 47));
 List<Person> result = repository.findByAgeGreaterThan(18);
  49. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Redis Cluster (outlook) 49 postponed to Gosling 0-5460 5461-10922 10923-16383 Slots: MOVED 8712 127.0.0.1:6380 key Key calculation and node assignment are part of the driver library.
  50. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ Some additional themes for Gosling (more to come!) 50 QueryDSL 4 4.1 Spring Base Redis Cluster Reactive Repositories SpEL enhancements ZRANGEBYLEX Dynamic Projections Key Value Store Support Custom Implementation 2.0 Dynamic JPA Fetch Graphs
  51. Unless otherwise indicated, these slides are © 2013-2015 Pivotal Software,

    Inc. and licensed under a Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/ 51 Learn More. Stay Connected. Twitter: twitter.com/springcentral YouTube: spring.io/video LinkedIn: spring.io/linkedin Google Plus: spring.io/gplus