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

Tech Talk - Persistence with Spring Data

Tech Talk - Persistence with Spring Data

Christoph Strobl

March 22, 2022
Tweet

More Decks by Christoph Strobl

Other Decks in Programming

Transcript

  1. ©2022 VMware, Inc. Persistence with Spring Data what it is

    and what comes next Christoph Strobl (he/him) Staff Engineer March 2022
  2. ©2022 VMware, Inc. 2 MongoDB Redis Elasticsearch Cassandra MariaDB DynamoDB

    Neo4j Couchbase CouchDB Greenplum Hazelcast H2 SQL Server Oracle Db2 Areospike OrientDB MySQL PostgreSQL ©2022 VMware, Inc.
  3. ©2022 VMware, Inc. 3 SELECT e FROM employee e WHERE

    e.firstName = :name JPA create().select().from(EMPLOYEE) .where(EMPLOYEE.FIRST_NAME).eq(name) .fetch() JOOQ db.employee.find({ first_name : name }) NoSQL SELECT * FROM employee WHERE first_name = %1 SQL MATCH (n:Employee) WHERE n.firstName = ’...’ query: { match: { first_name: '...' } }
  4. ©2022 VMware, Inc. 4 List<Employee> findByFirstName(String name) Spring Data JPA

    JDBC Redis Neo4j R2DBC MongoDB ArangoDB Couchbase Cosmos DB yugabyteDB Elasticsearch Apache Geode Apache Cassandra ...
  5. ©2022 VMware, Inc. 6 Anatomy Entity Manager Database Driver Template

    API Mapping Conversion Resource & Transaction Management native store format <> domain type eg. enum -> string Default Implementation Repository Interface List<Employee> findByFirstName(String name)
  6. ©2022 VMware, Inc. 7 Template APIs class MongoTemplate implements MongoOperations

    { <T> List<T> find(Query query, Class<T> entityClass) <T> List<T> findAndRemove(Query query, Class<T> entityClass) <T> T execute(Class<?> entityClass, CollectionCallback<T> callback) query(where(“firstName”).is(...)) template.execute(Employee.class, collection -> { return collection.find(new Document("firstname", "...")) .into(new ArrayList()); }); Access to native driver commands
  7. ©2022 VMware, Inc. 8 Repositories interface Repo<Employee, String> extends ...{

    Repository CrudRepository findAll(); findById(...) count(); save(...); delete(...); ... PagingAndSorting Repository findAll(Page); findAll(Sort); Proxy Default Implementation Store Specific List<Employee> findAll(); long count(); Predefined methods List<Employee> findByFirstName(String name); Derived Finder Method
  8. ©2022 VMware, Inc. 9 Derived Find Queries List<Employee> findEmployeeByFirstNameLikeOrderByAgeAsc(String name)

    read get query search stream Placeholder For Anything You Like matches contains lessThan startsWith greaterThan isEmpty isNull isNotNull before after ... Match Operator Sort Direction Delimiter Employee Page<Employee> Slice<Employee> Stream<Employee>
  9. ©2022 VMware, Inc. 10 Derived Count | Exists | Delete

    Queries int countEmployeeByFirstNameLike(String name) boolean existsEmployeeByFirstNameLike(String name) Some Stores Need a Little Help @Modifying List<Employee> deleteEmployeeByFirstNameLike(String name) remove void long Interested in the Operation Result?
  10. ©2022 VMware, Inc. 11 Data Projections List<View> findByFirstName(String name) Closed

    Interface Projection interface View2 { String getFirstName(); String getLastName(); } class View1 { String firstName; String lastName; } DTO Projection interface View3 { @Value(“#{target....}”) String getName(); } Open Interface Projection Flexible View On Your Data
  11. ©2022 VMware, Inc. 14 @Entity class Employee { @Id @GeneratedValue(...)

    Long id; @Column(name="FIRST_N") String firstName; ... } @Document class Employee { @Id Long id; @Field(name="first_n") String firstName; ... } JPA MongoDB
  12. ©2022 VMware, Inc. 15 class Employee { Long id; String

    firstName; String lastName; Manager manager; } class Manager { Long id; String name; List<Employee> employees } ID Name 1000 Marvin ID FIRST_N LAST_N MANAGER_ID 1 Ford Prefect 1000 table based manager employee linking by default { _id : 1000, name : Marvin, employees : [ { id : 1, firstName : Ford, lastName : Prefect } ... document store embedding by default
  13. ©2022 VMware, Inc. 16 Native Queries @Query(“SELECT m, COUNT(e) FROM

    Manager m JOIN m.employees e GROUP BY e.name”) List<View> listManagerWithEmployeeCount(); JPA @Aggregation(“{ $project: { id : 1, name: 1, $size : employees } }”) List<View> listManagerWithEmployeeCount(); MongoDB
  14. ©2022 VMware, Inc. 17 Richer / Slimmer API Read the

    Documentation for store specific features @Procedure(”Employee.increaseSalary”) Integer increaseSalary(Integer raise); JPA Stored Procedure call GeoResults<Employee> findByOfficeLoactionNear(Point p, Distance max) List Stream Geospatial Query @Update(”{ ... }”) Integer findAndIncreaseSalaryByName(String name, Integer raise); MongoDB Update
  15. ©2022 VMware, Inc. 18 interface Repo<Employee, String> extends Repository {

    ... } Custom Implementations interface MyExtension { Reward beKind(Person person); } class MyExtensionImpl implements MyExtension { ExtensionImpl(EntityManager em) { ... } public Reward beKind(Person person) { ... } } , MyExtension { repository fragment
  16. ©2022 VMware, Inc. 21 Spring Data provides a Familiar &

    Consistent Programming Model that resprects Store Specific Traits
  17. ©2022 VMware, Inc. 22 What’s next? Spring Data 2.x &

    3.0 The following is intended to outline the general direction of VMware's offerings. It is intended for information purposes only and may not be incorporated into any contract. Any information regarding pre-release of VMware offerings, future updates or other planned modifications is subject to ongoing evaluation by VMware and is subject to change. This information is provided without warranty or any kind, express or implied, and is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions regarding VMware's offerings. These purchasing decisions should only be based on features currently available. The development, release, and timing of any features or functionality described for VMware's offerings in this presentation remain at the sole discretion of VMware. VMware has no obligation to update forward looking information in this presentation
  18. ©2022 VMware, Inc. 23 2.x – Property Converters class Person

    { Long id; String name; String ssn; } @WritingConverter class PersonToDocumentConverter implements Converter<Person, Document> { public Document convert(Person source) { ... } } @ReadingConverter class DocumentToPersonConverter implements Converter<Document, Person> { public Person convert(Document source) { ... } }
  19. ©2022 VMware, Inc. 24 2.x – Property Converters class Person

    { Long id; String name; @ValueConverter(EncryptingValueConverter.class) String ssn; } declarative PropertyValueConverterRegistrar registrar = new ... registrar.registerConverter(Person.class, Person::getSsn) .writing(utils::encrypt) .reading(utils::decrypt); programmatic new in 2.x
  20. ©2022 VMware, Inc. 25 3.0 - Baseline & Upgrades •

    Java 17 • Jakarta EE 9 • Hibernate 6 • Validation API • CDI Extensions • ...
  21. ©2022 VMware, Inc. 26 3.0 - API Refinements Repository CrudRepository

    Iterable<T> findAll(); Iterable<T> saveAll(...); T findById(...) long count(); void delete(...); ... PagingAndSorting Repository Page<T> findAll(Page); ... ListCrudRepository List<T> findAll(); List<T> saveAll(...); T findById(...) long count(); void delete(...); ...
  22. ©2022 VMware, Inc. 27 3.0 - Observability AppOptics, Azure Monitor,

    Netflix Atlas, CloudWatch, Datadog, Dynatrace, Elastic, Ganglia, Graphite, Humio, Influx/Telegraf, JMX, KairosDB, New Relic, Prometheus, SignalFx, Google Stackdriver, StatsD, Wavefront
  23. ©2022 VMware, Inc. 28 3.0 - GraalVM native • static

    type hints • dynamic configuration processors