Slide 1

Slide 1 text

A Spring Data’s Guide to Persistence September 1–2, 2021 springone.io 1

Slide 2

Slide 2 text

Safe Harbor Statement 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. 2

Slide 3

Slide 3 text

DON‘T PANIC Douglas Adams, The Hitchhiker's Guide to the Galaxy

Slide 4

Slide 4 text

4 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 everything MATCH (n:Employee) WHERE n.firstName = ’...’ query: { match: { first_name: '...' } }

Slide 5

Slide 5 text

List findByFirstName(String name) Spring Data JPA JDBC Redis Neo4j R2DBC MongoDB ArangoDB Couchbase Cosmos DB yugabyteDB Elasticsearch Apache Geode Apache Cassandra ...

Slide 6

Slide 6 text

42 life – data - everything

Slide 7

Slide 7 text

7 A Familiar & Consistent Programming Model That Resprects Store Specific Traits no silver bullet no magic not one API to rule them all

Slide 8

Slide 8 text

The Basics

Slide 9

Slide 9 text

Anatomy 9 Entity Manager Database Driver Template API Mapping Conversion Resource & Transaction Management native store format <> domain type eg. enum -> string Default Implementation Repository Interface List findByFirstName(String name)

Slide 10

Slide 10 text

Template APIs 10 class MongoTemplate implements MongoOperations { List find(Query query, Class entityClass) List findAndRemove(Query query, Class entityClass) T execute(Class> entityClass, CollectionCallback callback) query(where(“firstName”).is(...)) template.execute(Employee.class, collection -> { return collection.find(new Document("firstname", "...")) .into(new ArrayList()); }); Access to native driver commands

Slide 11

Slide 11 text

Repositories 11 interface Repo extends ...{ Repository CrudRepository findAll(); findById(...) count(); save(...); delete(...); ... PagingAndSorting Repository findAll(Page); findAll(Sort); Proxy Default Implementation Store Specific List findAll(); long count(); Predefined methods List findByFirstName(String name); Derived Finder Method

Slide 12

Slide 12 text

Derived Queries 12 List findByFirstName(String name) Return Type Operation Keyword Match Against Bind Argument

Slide 13

Slide 13 text

Paging vs Streaming 13 Page findByFirstName(String name, Pageable page) Resource Allocation Per Invocation Stream findByFirstName(String name) Continuous Scrolling

Slide 14

Slide 14 text

Derived Fetch Queries 14 List 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 Slice Stream

Slide 15

Slide 15 text

Derived Count | Exists Queries 15 int countEmployeeByFirstNameLike(String name) boolean existsEmployeeByFirstNameLike(String name)

Slide 16

Slide 16 text

Derived Delete Queries 16 @Modifying List deleteEmployeeByFirstNameLike(String name) remove Some Stores Need a Little Help void long Interested in the Operation Result?

Slide 17

Slide 17 text

17 I‘d far rather be happy than right any day. IDE Support Runtime Support Douglas Adams, The Hitchhiker's Guide to the Galaxy

Slide 18

Slide 18 text

Data Projections 18 List 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

Slide 19

Slide 19 text

Query By Example 19 List findAll(Example probe) a Good Fit for Web Form Search Binding Page Stream long count Pageable Sort

Slide 20

Slide 20 text

Auditing 2 0 class Employee { String firstName; String lastName; @CreatedDate Instant created; @LastModifiedDate Instant lastModified; }

Slide 21

Slide 21 text

Reactive 21 Flux findByFirstName(String name) a Reactive Stream of Data For a moment, nothing happened. Then, after a second or so, nothing continued to happen. once you forget to subscribe to the stream Douglas Adams, The Hitchhiker's Guide to the Galaxy

Slide 22

Slide 22 text

Storage Engine Store Specific

Slide 23

Slide 23 text

23 class Employee { Long id; String firstName; String lastName; Manager manager; } class Manager { Long id; String name; List employees } ID Name 1000 Marvin ID FIRST_N LAST_N MANAGER_ID 1 Ford Perfect 1000 table based manager employee linking by default { _id : 1000, name : Marvin, employees : [ { id : 1, firstName : Ford, lastName : Perfect } ... document store embedding by default

Slide 24

Slide 24 text

2 4 Would it save you a lot of time if I just gave up and went mad now? Actually, it’s not that bad once you got the differences. Decide on what you need. Not what you got or think to be cool. Douglas Adams, The Hitchhiker's Guide to the Galaxy

Slide 25

Slide 25 text

25 @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

Slide 26

Slide 26 text

26 @Document class Manager { Long id; String name; @DBRef List employees } { _id : 1000, name : Marvin, employees : [ { $ref : employee, $id : 1 } ... native $dbref format MongoDB – Linking manager { _id : 1, ..., manager : 1000 } @Document class Employee { Long id; //... @DocumentReference Manager manager; } more common simple format employee

Slide 27

Slide 27 text

27 @Entity class Employee { Long id; //... @Embedded @AttributeOverrides({...}) Manager manager; } JPA – Embedding ID FIRST_N LAST_N MANAGER_ID MANAGER_NAME 1 Ford Perfect 1000 Marvin

Slide 28

Slide 28 text

Query Language Store Specific

Slide 29

Slide 29 text

Richer / Slimmer API 2 9 Read the Documentation for store specific features @Procedure(”Employee.increaseSalary”) Integer increaseSalary(Integer raise); JPA Stored Procedure call GeoResults findByOfficeLoactionNear(Point p, Distance max) List Stream MongoDB Geospatial Query

Slide 30

Slide 30 text

Native Queries 3 0 @Query(“SELECT m, COUNT(e) FROM Manager m JOIN m.employees e GROUP BY e.name”) List listManagerWithEmployeeCount(); JPA @Aggregation(“{ $project: { id : 1, name: 1, $size : employees } }”) List listManagerWithEmployeeCount(); MongoDB

Slide 31

Slide 31 text

Custom Implementations Store Specific

Slide 32

Slide 32 text

Repositories 3 2 interface Repo extends ...{ CrudRepository Repository PagingAndSorting Repository List findAll(); long count(); findAll(); findById(...) count(); save(...); delete(...); ... findAll(Page); findAll(Sort); Default Implementation Proxy Store Specific

Slide 33

Slide 33 text

Repository Fragments 3 3 interface Repo extends ...{ List findAll(); long count(); Default Implementation Proxy Repository CrudRepository PagingAndSorting Repository Fragment I Fragment II Fragment III Fragment Impl Custom Method Declaration invocation target

Slide 34

Slide 34 text

Time is an illusion. Lunchtime doubly so. 3 4 Bad Performance? Slow Queries? Keep calm and log Douglas Adams, The Hitchhiker's Guide to the Galaxy

Slide 35

Slide 35 text

Trouble Shooting 3 5 Repository Metrics Logfiles Network Stats Query Planners

Slide 36

Slide 36 text

Guides & Samples 3 6 spring.io/guides spring-projects/spring-data-examples

Slide 37

Slide 37 text

#springone @SpringOne So long, and thanks for all the fish. Douglas Adams, The Hitchhiker's Guide to the Galaxy Noooo - Wait, there’s more...! Mapping Conversion Auditing Event Listeners Entity Callbacks Transactions Live Coding Spring Data Queries to the End of the Persistence Universe