Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The New Kid on the Block: Spring Data JDBC - Jens Schauder

321 views

Published on

The New Kid on the Block: Spring Data JDBC by Jens Schauder at SpringOne Tour 2019

Published in: Software
  • Be the first to comment

  • Be the first to like this

The New Kid on the Block: Spring Data JDBC - Jens Schauder

  1. 1. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 1/61 Spring Data JDBCSpring Data JDBC
  2. 2. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 2/61 Spring DataSpring Data
  3. 3. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 3/61 Let’s take a lookLet’s take a look a.k.a Demoa.k.a Demo
  4. 4. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 4/61 You needYou need spring-boot-starter-data-spring-boot-starter-data- jdbcjdbc Aggregate Roots need anAggregate Roots need an @Id@Id No getters/setter requiredNo getters/setter required You can "wither" insteadYou can "wither" instead
  5. 5. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 5/61 Why Spring Data Jdbc?Why Spring Data Jdbc? We have already JPAWe have already JPA
  6. 6. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 6/61 JPAs complexityJPAs complexity
  7. 7. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 7/61 Lazy Loading (Exception)Lazy Loading (Exception)
  8. 8. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 8/61 Dirty CheckingDirty Checking
  9. 9. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 9/61 Session / 1st Level CacheSession / 1st Level Cache
  10. 10. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 10/61 Proxies for EntitiesProxies for Entities
  11. 11. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 11/61 Map almost anything to anythingMap almost anything to anything
  12. 12. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 12/61 Spring Data JDBCs Design ChoicesSpring Data JDBCs Design Choices
  13. 13. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 13/61 No Lazy LoadingNo Lazy Loading
  14. 14. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 14/61 No CachingNo Caching
  15. 15. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 15/61 No ProxiesNo Proxies
  16. 16. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 16/61 No deferred flushingNo deferred flushing
  17. 17. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 17/61 Very simple & limited & opinionatedVery simple & limited & opinionated ORMORM
  18. 18. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 18/61 Simplicity is KingSimplicity is King Complexity is temptingComplexity is tempting
  19. 19. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 19/61 Let’s see that in actionLet’s see that in action a.k.a more demo codea.k.a more demo code
  20. 20. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 20/61 Clone: set ID toClone: set ID to nullnull and save again.and save again. Loading the same entity creates multipleLoading the same entity creates multiple instances.instances.
  21. 21. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 21/61 Death is for freeDeath is for free and it costs you your life.and it costs you your life.
  22. 22. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 22/61 Non trivial AggregatesNon trivial Aggregates
  23. 23. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 23/61 Design ChallengeDesign Challenge CRUD Operations for entities with referencesCRUD Operations for entities with references
  24. 24. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 24/61 more Demomore Demo
  25. 25. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 25/61 One to N is fineOne to N is fine But what about Many-to-X?But what about Many-to-X?
  26. 26. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 26/61 ⇒No Many-to-X relations!⇒No Many-to-X relations!
  27. 27. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 27/61 AggregateAggregate https://martinfowler.com/bliki/DDD_Aggregate.html — Martin Fowler Aggregate is a pattern in Domain-DrivenAggregate is a pattern in Domain-Driven Design. A DDD aggregate is a cluster ofDesign. A DDD aggregate is a cluster of domain objects that can be treated as adomain objects that can be treated as a single unit.single unit.
  28. 28. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 28/61 Repositories work on AggregatesRepositories work on Aggregates https://martinfowler.com/bliki/DDD_Aggregate.html — Martin Fowler Aggregates are the basic element ofAggregates are the basic element of transfer of data storage - you request totransfer of data storage - you request to load or save whole aggregates.load or save whole aggregates.
  29. 29. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 29/61 Aggregate RootAggregate Root https://martinfowler.com/bliki/DDD_Aggregate.html — Martin Fowler An aggregate will have one of itsAn aggregate will have one of its component objects be the aggregate root.component objects be the aggregate root. Any references from outside the aggregateAny references from outside the aggregate should only go to the aggregate root. Theshould only go to the aggregate root. The root can thus ensure the integrity of theroot can thus ensure the integrity of the aggregate as a whole.aggregate as a whole.
  30. 30. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 30/61 Many to X relationsMany to X relations If multiple Aggregate roots reference X,If multiple Aggregate roots reference X, X must be another Aggregate Root.X must be another Aggregate Root. ⇒Not part of this Aggregate.⇒Not part of this Aggregate.
  31. 31. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 31/61 What is part of the Aggregate?What is part of the Aggregate? Everything referenced.Everything referenced.
  32. 32. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 32/61 But Many-to-X is real!But Many-to-X is real! Just reference anJust reference an id.id.
  33. 33. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 33/61 Let’s look at codeLet’s look at code
  34. 34. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 34/61 EffectsEffects Makes Aggregates obvious in codeMakes Aggregates obvious in code Easier testing (no huge chains of dependencies)Easier testing (no huge chains of dependencies) Allows to pick a persistence strategy perAllows to pick a persistence strategy per AggregateAggregate
  35. 35. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 35/61 EventsEvents
  36. 36. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 36/61 Available EventsAvailable Events BeforeSaveEventBeforeSaveEvent AfterSaveEventAfterSaveEvent BeforeDeleteEventBeforeDeleteEvent AfterDeleteEventAfterDeleteEvent AfterLoadEventAfterLoadEvent - when an Aggregate got instantiated- when an Aggregate got instantiated from the databasefrom the database
  37. 37. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 37/61 AggregateChangeAggregateChange A model of the changes to be applied to theA model of the changes to be applied to the databasedatabase contains a list ofcontains a list of DbActionDbAction s.s.
  38. 38. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 38/61 DbActionDbAction Represents a single SQL statement to be executed againstRepresents a single SQL statement to be executed against the database.the database.
  39. 39. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 39/61 Do whatever you wantDo whatever you want Access theAccess the repositoryrepository change the entitieschange the entities
  40. 40. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 40/61 AuditingAuditing
  41. 41. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 41/61 Enable Auditing SupportEnable Auditing Support @EnableJdbcAuditing@EnableJdbcAuditing
  42. 42. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 42/61 Add fieldsAdd fields @CreatedBy String createdBy; @CreatedDate Instant createdAt; @LastModifiedBy String updatedBy; @LastModifiedDate Instant updatedAt;
  43. 43. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 43/61 RegisterRegister AuditorAwareAuditorAware @Bean AuditorAware<String> auditorAware() { return new ModifiableAuditorAware(); }
  44. 44. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 44/61 AuditorAwareAuditorAware exampleexample public class ModifiableAuditorAware implements AuditorAware<String> { String value; @Override public Optional<String> getCurrentAuditor() { return Optional.ofNullable(value); } }
  45. 45. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 45/61 MyBatisMyBatis
  46. 46. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 46/61 @Query@Query
  47. 47. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 47/61 @Query@Query @Query("select count(*) from address") int countAddresses();
  48. 48. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 48/61 @Modifying@Modifying
  49. 49. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 49/61 RowMapper / ResultSetExtractorRowMapper / ResultSetExtractor
  50. 50. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 50/61 (Maybe) upcoming(Maybe) upcoming Not promising anything.Not promising anything.
  51. 51. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 51/61 Better CRUDBetter CRUD upserts?upserts?
  52. 52. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 52/61 Derived QueriesDerived Queries
  53. 53. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 53/61 Sorting and PagingSorting and Paging
  54. 54. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 54/61 Named Queries with MyBatisNamed Queries with MyBatis
  55. 55. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 55/61 Named Queries via PropertiesNamed Queries via Properties
  56. 56. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 56/61 Is there reactive support?Is there reactive support?
  57. 57. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 57/61 Not in Spring Data JDBCNot in Spring Data JDBC But in Spring Data R2DBC (not GA yet)But in Spring Data R2DBC (not GA yet)
  58. 58. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 58/61 ReferencesReferences Github:Github: Project:Project: Examples:Examples: Slides on Github:Slides on Github: https://github.com/spring-projects/spring-data-https://github.com/spring-projects/spring-data- jdbcjdbc https://projects.spring.io/spring-data-jdbc/https://projects.spring.io/spring-data-jdbc/ https://github.com/spring-projects/spring-data-https://github.com/spring-projects/spring-data- examples/tree/master/jdbcexamples/tree/master/jdbc https://github.com/schauder/talk-spring-https://github.com/schauder/talk-spring- data-jdbcdata-jdbc
  59. 59. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 59/61 More ReferencesMore References Blog article 1:Blog article 1: Blog article 2:Blog article 2: R2DBC:R2DBC: Spring Data R2DBC:Spring Data R2DBC: https://spring.io/blog/2018/09/17/introducing-spring-https://spring.io/blog/2018/09/17/introducing-spring- data-jdbcdata-jdbc https://spring.io/blog/2018/09/24/spring-https://spring.io/blog/2018/09/24/spring- data-jdbc-references-and-aggregatesdata-jdbc-references-and-aggregates https://r2dbc.io/https://r2dbc.io/ https://spring.io/projects/spring-data-https://spring.io/projects/spring-data- r2dbcr2dbc
  60. 60. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 60/61 Jens SchauderJens Schauder Dad, Board&Roleplaying Gamer, Runner, Bouldering,Dad, Board&Roleplaying Gamer, Runner, Bouldering, Freeletics MasochistFreeletics Masochist + JUG OrganizerJUG Organizer Slides online: http://bit.ly/talk-sd-jdbcSlides online: http://bit.ly/talk-sd-jdbc Print as PDFPrint as PDF
  61. 61. 27/04/2019 Spring Data JDBC file:///Users/jschauder/IdeaProjects/talk-spring-data-jdbc/docs/talk.html?print-pdf#/ 61/61 @jensschauder #SpringOneTour #sdJdbc Questions?Questions?

×