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.

An Introduction to jOOQ

1,702 views

Published on

Are you tired of Hibernate? Is GORM is too heavy for your current project? Do you like having more control over your SQL? Do you like flexible DSLs? Try JOOQ!

JOOQ (Java Object Oriented Querying) is light-weight alternative to classic data access solutions or ORMs like Hibernate, JPA, JDBC, and GORM. JOOQ's goal is to give the developer a flexible DSL for building typesafe, database agnostic SQL queries, and attempts to convince the developer of a ‘database-first’ approach to building their application. In this talk we’ll quickly present an introduction to JOOQ from a high level, discuss its features, and see several examples of how we’re using JOOQ to great effect with many Spring Boot and Ratpack apps within our platform.

Published in: Software

An Introduction to jOOQ

  1. 1. An Introduction to JOOQ (or, Long Live SQL) Steve Pember @svpember Gr8Conf US, 2016 THIRDCHANNEL @svpember
  2. 2. You may find this to be terribly boring.
  3. 3. THIRDCHANNEL @svpember Agenda • Growing beyond Object Relational Mappers • An Introduction to jOOQ • What We’ve Learned (A.K.A. jOOQ and Groovy) • Demo / Code Samples
  4. 4. The Case for SQL
  5. 5. ORMs are Unnecessary
  6. 6. This debate is not new.
  7. 7. ORMs Can Save You Time…
  8. 8. JPA, anyone?
  9. 9. THIRDCHANNEL @svpember
  10. 10. … but they have flaws.
  11. 11. THIRDCHANNEL @svpember ORM Difficulties • Object / Relational Impedance Mismatch
  12. 12. Objects: identity, state, behavior RDBMS: attribute storage, retrieved by predicate logic
  13. 13. THIRDCHANNEL @svpember
  14. 14. THIRDCHANNEL @svpember ORM Difficulties • Object / Relational Impedance Mismatch • ORM Oriented Schema Design
  15. 15. THIRDCHANNEL @svpember
  16. 16. THIRDCHANNEL @svpember
  17. 17. THIRDCHANNEL @svpember
  18. 18. Wonderful.
  19. 19. Single Table vs Multi Table Inheritance
  20. 20. THIRDCHANNEL @svpember ORM Difficulties • Object / Relational Impedance Mismatch • ORM Oriented Schema Design • Trouble with Complicated Queries
  21. 21. E.G. Criteria, HQL , @Query, etc.
  22. 22. THIRDCHANNEL @svpember
  23. 23. THIRDCHANNEL @svpember ORM Difficulties • Object / Relational Impedance Mismatch • ORM Oriented Schema Design • Trouble with Complicated Queries • Resource Usage
  24. 24. Maintain That Entity Graph
  25. 25. THIRDCHANNEL @svpember ORM Difficulties • Object / Relational Impedance Mismatch • ORM Oriented Schema Design • Trouble with Complicated Queries • Resource Usage • Performance
  26. 26. To Re-phrase My Original Point:
  27. 27. ORMs Make You Weak
  28. 28. THIRDCHANNEL @svpember
  29. 29. SQL makes you Strong
  30. 30. THIRDCHANNEL @svpember So What To Do?
  31. 31. How about some raw JDBC?!
  32. 32. Problems?
  33. 33. THIRDCHANNEL @svpember What Was Wrong?
  34. 34. Enter: jOOQ!
  35. 35. THIRDCHANNEL @svpember Agenda • Growing beyond Object Relational Mappers • An Introduction to jOOQ
  36. 36. THIRDCHANNEL @svpember jOOQ (Intro) • Library for SQL interaction
  37. 37. THIRDCHANNEL @svpember jOOQ (Intro) • Library for SQL interaction • For people not afraid of SQL
  38. 38. THIRDCHANNEL @svpember jOOQ (Intro) • Library for SQL interaction • For people not afraid of SQL • The jOOQ authors LOVE SQL
  39. 39. And they are eager to help
  40. 40. THIRDCHANNEL @svpember jOOQ (Intro) • Library for SQL interaction • For people not afraid of SQL • The jOOQ authors LOVE SQL • Database should drive internal models
  41. 41. jOOQ Features
  42. 42. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL
  43. 43. THIRDCHANNEL @svpember
  44. 44. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL • Fluent API
  45. 45. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL • Fluent API • Code Generation
  46. 46. THIRDCHANNEL @svpember
  47. 47. –Johnny Appleseed “Type a quote here.”
  48. 48. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL • Fluent API • Code Generation • Active Record
  49. 49. THIRDCHANNEL @svpember
  50. 50. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL • Fluent API • Code Generation • Active Record • Full SQL Support
  51. 51. Even Vendor Specific Operations?
  52. 52. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL • Fluent API • Code Generation • Active Record • Full SQL Support • Native Support of Stored Procedures
  53. 53. THIRDCHANNEL @svpember jOOQ (Features) • Type-Safe SQL • Fluent API • Code Generation • Active Record • Full SQL Support • Native Support of Stored Procedures • Open Source and Free
  54. 54. Unless You’re Using Oracle…
  55. 55. THIRDCHANNEL @svpember Code Overview
  56. 56. Don’t want to take up too much of your time…
  57. 57. But let’s talk about Fetching
  58. 58. THIRDCHANNEL @svpember Fetch • fetchOne
  59. 59. THIRDCHANNEL @svpember
  60. 60. THIRDCHANNEL @svpember
  61. 61. THIRDCHANNEL @svpember Fetch • fetchOne • fetchAny
  62. 62. THIRDCHANNEL @svpember
  63. 63. THIRDCHANNEL @svpember Fetch • fetchOne • fetchAny • fetchLazy
  64. 64. THIRDCHANNEL @svpember
  65. 65. THIRDCHANNEL @svpember Fetch • fetchOne • fetchAny • fetchLazy • fetchLater
  66. 66. fetchLater -> returns a Promise… but is gone in 4.0
  67. 67. THIRDCHANNEL @svpember Fetch • fetchOne • fetchAny • fetchLazy • fetchLater • fetchMany
  68. 68. fetchMany() -> you have stored procedures with multiple result sets
  69. 69. THIRDCHANNEL @svpember Fetch • fetchOne • fetchAny • fetchLazy • fetchLater • fetchMany • fetchInto
  70. 70. fetchInto() is quite flexible
  71. 71. Fetch into Records! Fetch into POGOs!
  72. 72. Fetching into POGOs keeps layers separate
  73. 73. THIRDCHANNEL @svpember
  74. 74. THIRDCHANNEL @svpember
  75. 75. –Johnny Appleseed “Type a quote here.”
  76. 76. And with Java 8…
  77. 77. Fetch Into Streams!
  78. 78. THIRDCHANNEL @svpember Agenda • Growing beyond Object Relational Mappers • An Introduction to jOOQ • What We’ve Learned (A.K.A. jOOQ and Groovy)
  79. 79. THIRDCHANNEL @svpember What We’ve Learned • Code Generation is Not Mandatory
  80. 80. THIRDCHANNEL @svpember
  81. 81. THIRDCHANNEL @svpember What We’ve Learned • Code Generation is Not Mandatory • jOOQ Does Not Manage Transactions For You!
  82. 82. THIRDCHANNEL @svpember What We’ve Learned • Code Generation is Not Mandatory • jOOQ Does Not Manage Transactions For You! • Import / Export Direct to CSV
  83. 83. THIRDCHANNEL @svpember What We’ve Learned • Code Generation is Not Mandatory • jOOQ Does Not Manage Transactions For You! • Import / Export Direct to CSV • Easy & Clear Logging
  84. 84. THIRDCHANNEL @svpember What We’ve Learned • Code Generation is Not Mandatory • jOOQ Does Not Manage Transactions For You! • Import / Export Direct to CSV • Easy & Clear Logging • Binders / Casting
  85. 85. –Johnny Appleseed “Type a quote here.”
  86. 86. THIRDCHANNEL @svpember
  87. 87. THIRDCHANNEL @svpember
  88. 88. THIRDCHANNEL @svpember What We’ve Learned • Code Generation is Not Mandatory • jOOQ Does Not Manage Transactions For You! • Import / Export Direct to CSV • Easy & Clear Logging • Binders / Casting • jOOQ & CompileStatic
  89. 89. THIRDCHANNEL @svpember
  90. 90. THIRDCHANNEL @svpember Agenda • Growing beyond Object Relational Mappers • An Introduction to jOOQ • What We’ve Learned (A.K.A. jOOQ and Groovy) • Demo / Code Samples
  91. 91. Thank You! Steve Pember @svpember THIRDCHANNEL @svpember
  92. 92. THIRDCHANNEL @svpember More Info • jOOQ Blog: https://blog.jooq.org/ • Vietnam of Computer Science: http://blogs.tedneward.com/post/the-vietnam-of-computer-science/ • Fluent API: http://martinfowler.com/bliki/FluentInterface.html • jOOQ Gradle Plugin: https://github.com/etiennestuder/gradle-jooq-plugin • Code Demo: https://github.com/spember/jooq-demo
  93. 93. THIRDCHANNEL @svpember Images • Brave Puffin: https://memegenerator.net/instance/63187852 • Weak(Gravity Falls): http://giphy.com/gifs/weak-gravity-falls-sTm3FmD7knvO0 • Strong (Spongebob): http://www.reactiongifs.com/strongbob/?utm_source=rss&utm_medium=rss&utm_campaign=strongbob • Andy Dwyer (No Idea): http://memecaptain.com/gend_image_pages/iepvqA • Atticus Finch: http://www.latimes.com/entertainment/movies/la-et-mn-atticus-white-savior-20150715-story.html • Yikes (Adam Scott): http://giphy.com/gifs/awkward-yikes-adam-scott-srFijDgoNj008 • Suspicious Fry: http://imgur.com/gallery/OCcno • Strong (Spongebob): http://www.reactiongifs.com/strongbob/?utm_source=rss&utm_medium=rss&utm_campaign=strongbob

×