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

    View Slide

  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.

    View Slide

  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: '...' } }

    View Slide

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

    View Slide

  5. ©2022 VMware, Inc. 5
    No Magic

    View Slide

  6. ©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 findByFirstName(String name)

    View Slide

  7. ©2022 VMware, Inc. 7
    Template APIs
    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

    View Slide

  8. ©2022 VMware, Inc. 8
    Repositories
    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

    View Slide

  9. ©2022 VMware, Inc. 9
    Derived Find Queries
    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

    View Slide

  10. ©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 deleteEmployeeByFirstNameLike(String name)
    remove
    void
    long
    Interested in the
    Operation Result?

    View Slide

  11. ©2022 VMware, Inc. 11
    Data Projections
    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

    View Slide

  12. ©2022 VMware, Inc. 12
    Reactive
    Flux findByFirstName(String name)
    a Reactive Stream of Data

    View Slide

  13. ©2022 VMware, Inc. 13
    Not one API
    to rule them all

    View Slide

  14. ©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

    View Slide

  15. ©2022 VMware, Inc. 15
    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 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

    View Slide

  16. ©2022 VMware, Inc. 16
    Native Queries
    @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

    View Slide

  17. ©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 findByOfficeLoactionNear(Point p, Distance max)
    List
    Stream
    Geospatial Query
    @Update(”{ ... }”)
    Integer findAndIncreaseSalaryByName(String name, Integer raise);
    MongoDB Update

    View Slide

  18. ©2022 VMware, Inc. 18
    interface Repo 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

    View Slide

  19. ©2022 VMware, Inc. 19
    No Silver Bullet

    View Slide

  20. ©2022 VMware, Inc. 20
    Trouble Shooting
    Repository Metrics
    Logfiles
    Network Stats
    Query Planners

    View Slide

  21. ©2022 VMware, Inc. 21
    Spring Data provides a
    Familiar & Consistent
    Programming Model
    that resprects
    Store Specific Traits

    View Slide

  22. ©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

    View Slide

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

    View Slide

  24. ©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

    View Slide

  25. ©2022 VMware, Inc. 25
    3.0 - Baseline & Upgrades
    • Java 17
    • Jakarta EE 9
    • Hibernate 6
    • Validation API
    • CDI Extensions
    • ...

    View Slide

  26. ©2022 VMware, Inc. 26
    3.0 - API Refinements
    Repository
    CrudRepository
    Iterable findAll();
    Iterable saveAll(...);
    T findById(...)
    long count();
    void delete(...);
    ...
    PagingAndSorting
    Repository
    Page findAll(Page);
    ...
    ListCrudRepository
    List findAll();
    List saveAll(...);
    T findById(...)
    long count();
    void delete(...);
    ...

    View Slide

  27. ©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

    View Slide

  28. ©2022 VMware, Inc. 28
    3.0 - GraalVM native
    • static type hints
    • dynamic configuration
    processors

    View Slide

  29. Thank You
    ©2022 VMware, Inc.

    View Slide