23024
23024
com
https://ebookmeta.com/product/beginning-jooq-learn-to-write-
efficient-and-effective-java-based-sql-database-
operations-1st-edition-tayo-koleoso/
OR CLICK HERE
DOWLOAD EBOOK
ebookmeta.com
https://ebookmeta.com/product/runequest-cults-of-runequest-the-
prosopaedia-1st-edition-greg-stafford/
ebookmeta.com
The 2021 International Conference on Machine Learning and
Big Data Analytics for IoT Security and Privacy:
SPIoT-2021 Volume 2 (Lecture Notes on Data Engineering and
Communications Technologies, 98) John Macintyre (Editor)
https://ebookmeta.com/product/the-2021-international-conference-on-
machine-learning-and-big-data-analytics-for-iot-security-and-privacy-
spiot-2021-volume-2-lecture-notes-on-data-engineering-and-
communications-technologies-98-j/
ebookmeta.com
https://ebookmeta.com/product/flame-on-you-1st-edition-taiden-milly/
ebookmeta.com
https://ebookmeta.com/product/pocket-guide-to-quilting-tips-
tricks-4th-edition-haren-penny/
ebookmeta.com
https://ebookmeta.com/product/sinners-fairytales-06-0-lust-alexandra-
k-martin/
ebookmeta.com
On the Wandering Paths 1st Edition Sylvain Tesson
https://ebookmeta.com/product/on-the-wandering-paths-1st-edition-
sylvain-tesson/
ebookmeta.com
Beginning
jOOQ
Learn to Write Efficient and Effective
Java-Based SQL Database Operations
—
Tayo Koleoso
Beginning jOOQ
Learn to Write Efficient
and Effective Java-Based SQL
Database Operations
Tayo Koleoso
Beginning jOOQ: Learn to Write Efficient and Effective Java-Based SQL
Database Operations
Tayo Koleoso
Silver Spring, MD, USA
iii
Table of Contents
Select Statements�����������������������������������������������������������������������������������������34
Insert Statements������������������������������������������������������������������������������������������63
Update Statements����������������������������������������������������������������������������������������66
Delete Statements�����������������������������������������������������������������������������������������67
Alternative Data Access Modes���������������������������������������������������������������������69
Transactions��������������������������������������������������������������������������������������������������������73
With Locking��������������������������������������������������������������������������������������������������76
Configuration������������������������������������������������������������������������������������������������������78
Connection Management�������������������������������������������������������������������������������79
Schema, Catalog, and Multi-tenant Deployment�������������������������������������������81
Query Management���������������������������������������������������������������������������������������84
Query Lifecycle Integration����������������������������������������������������������������������������86
iv
Table of Contents
Index�������������������������������������������������������������������������������������������������211
v
About the Author
Tayo Koleoso is the Founder and CEO of
LettuceWork (www.lettucework.io), the
platform dedicated to engineering culture.
He created the Better Managed Development
method for building and sustaining an effective
product engineering culture. He's a lifelong
learner, engineer, and engineering leader
committed to building people and software in
a healthy, sustainable, and effective ecosystem.
Outside of tech, comedy is the only thing he
consumes in large quantity. King of the Hill,
Peep Show and 30 Rock are his comfort telly,
I tell you what.
He got his start in software engineering as a teenage database
programmer with Oracle 8i. The jOOQ platform is therefore a natural fit and
a return to his roots: his love affair with SQL.
vii
About the Technical Reviewer
Vishwesh Ravi Shrimali graduated in 2018
from BITS Pilani, where he studied mechanical
engineering. Currently, he is working at
Mercedes Benz Research and Development
India Pvt. Ltd. as an ADAS Engineer. He has
also co-authored Machine Learning for
OpenCV 4 (Second Edition), The Computer
Vision Workshop, and Data Science for
Marketing Analytics (Second Edition) by Packt.
When he is not writing blogs or working on
projects, he likes to go on long walks or play
his acoustic guitar.
ix
CHAPTER 1
Welcome to jOOQ
I got my start in software engineering (and really, serious computer business)
at 15 years old, with Oracle 8i SQL. Yes, I’ve been an old man from a young
age, technologically speaking. Playing with SQL* Plus, trying (and failing) my
first Oracle SQL certification exam, before I even started university, taught me
the value of getting SQL right. Don’t take it from me, take it from this chap:
That tall drink of SQL? Probably me; I don’t know. The point is I deeply
appreciate Structured Query Language (SQL) and all it has to offer. The
industry’s been going gaga about NoSQL because it’s “easy to use” and it
“scales quickly,” but the fact of the matter is that SQL is still the undisputed
king of Online Analytical Processing (OLAP). When you want sanity
and integrity in your data, SQL is there. When you want (most of 2) the
1
Editor’s note: Oh brother. Here we go again.
2
I say “most of” here because different Relational Database Management Systems
provide varying degrees of ACID strength.
• Connection management
2
Chapter 1 Welcome to jOOQ
• Transaction management
3
Chapter 1 Welcome to jOOQ
4
Chapter 1 Welcome to jOOQ
have Java or Kotlin Spring Boot back ends, but fronted by not Java. Because
when you care about performance and resource efficiency in a domain like
the browser, nothing beats raw JavaScript.
And I say this as someone that’s spent a fair bit of time teaching about
JSF and answering questions on StackOverflow. Don’t get me wrong: JSF
is super convenient. Heck, that’s why I got into the business of JSF in the
first place: a cheap, convenient, and practical way to belch out markup and
scripting into a web page. But when no one’s watching, I know. I know that
raw JavaScript is still where it’s at. If you want to make your browser dance,
deal with the quirks and nuances of individual browsers, you turn to the
language invented for browsers. These hips stylesheets don’t lie.
Yet here we are, where many have decided that SQL should take a
backseat when interacting with databases. Park the language built for the
platform in the garage; Java, the language of kings, is preferable. In many
scenarios, it isn’t. Here are some reasons why:
3
Coming soon with Hibernate 6: https://docs.jboss.org/hibernate/orm/6.0/
userguide/html_single/Hibernate_User_Guide.html#hql-order-by
5
Chapter 1 Welcome to jOOQ
6
Chapter 1 Welcome to jOOQ
7
Chapter 1 Welcome to jOOQ
4
https://vladmihalcea.com/postgresql-serial-column-hibernate-identity/
5
https://vladmihalcea.com/whats-new-in-jpa-2-2-stream-the-result-of-
a-query-execution/
6
http://hibernate.org/orm/what-is-an-orm/
8
Chapter 1 Welcome to jOOQ
9
Chapter 1 Welcome to jOOQ
7
https://blog.jooq.org/2015/04/14/jooq-tuesdays-vlad-mihalcea-gives-
deep-insight-into-sql-and-hibernate/
8
Editor’s note: How many people do you think are concurrently reading a single
copy of your book?
10
Chapter 1 Welcome to jOOQ
D
atabase Aware
jOOQ is unapologetically RDBMS vendor aware.9 Unlike those other guys,
jOOQ provides API kits built specifically for a wide range of major database
vendors. This way, when you’re using jOOQ tools and APIs, you can be
sure you’re idiomatically correct; the syntax and semantics of your code is
transparently optimized and tightly mated with your database of choice.
And the delightful part of this is that it doesn’t translate to a tight coupling
to your Java code. Because jOOQ skews closer to the database, your Java
code can still remain technically database agnostic; your jOOQ library
implementation will quietly handle the finer details and nuances of your
specific database of choice. You can then quite easily flip a configuration in
jOOQ to support another, should you choose to switch databases.
C
ode Generation
jOOQ is all about convenience when it comes to the developer experience.
Observe.
Starting with any one of the following:
• Database table
• Sequence
• JPA entity
• XML file
9
www.jooq.org/download/support-matrix
11
Chapter 1 Welcome to jOOQ
Type Safety
All the entities, data, and fields you’ll be working with are typesafe –
guaranteed correct. The fields from your database tables, bind variables
from your stored procedures and functions, etc. are translated to the
closest match available in Java.
Domain-Specific Language
jOOQ provides a DSL that mirrors the specific SQL dialect that’s
implemented by your choice of database vendor. Even beyond that, the
library integrates the quirks and non-standard features of the supported
database vendors. You know the ones, the bad boy databases (looking at
you, Oracle). This compile-time checking of your SQL syntax means that
you’ll have to go out of your way to write broken or otherwise unsupported
SQL. No more waiting till runtime to find out your SQL doesn’t compile!
12
Chapter 1 Welcome to jOOQ
Tooling Support
You also get a robust toolkit to support a lot of developer convenience and
safe usage of the library:
• Flyway and Liquibase: You can use the two most popular
DML and DDL version control libraries with jOOQ.
13
Chapter 1 Welcome to jOOQ
JVM Languages
We get to take the jOOQ box on tour! You can use jOOQ with
• Kotlin
• Scala
• Groovy
This has been a 50,000 ft. flyover of jOOQ and what it brings to the
table. I want to stress here that jOOQ isn’t a replacement for ORM – the
right tool for the right job, etc. Where Hibernate, etc. don’t give you the
coverage you need, that’s where jOOQ swings into action.
So! Whaddya think so far? Pretty neat, huh? I’ll just catch my breath for
a moment here, and see you in the next chapter!
14
CHAPTER 2
Getting Started
with jOOQ
Let’s take this thing for spin! But first, you should get to know these classes
and interfaces; they’re at the heart of 75% of what you’ll be doing in jOOQ:
1. org.jooq.impl.DSL
• Stateful components
16
Chapter 2 Getting Started with jOOQ
3. org.jooq.conf.Settings
4. org.jooq.Configuration
1
Unlike with standard usage of PreparedStatement vs. Statement, you’re at no
greater risk of SQL injection by selecting one or the other in jOOQ. The difference
here is largely performance related, where PreparedStatement queries are
cacheable by the RDBMs.
17
Chapter 2 Getting Started with jOOQ
5. org.jooq.meta.jaxb.Configuration
18
Chapter 2 Getting Started with jOOQ
We’re going to use a relatively simplified data model here just for the
purpose of demonstrating specific jOOQ features. Here’s what that looks like.
19
Chapter 2 Getting Started with jOOQ
To help run some of the examples in this book, you can bootstrap with
the database definition that comes with the code sample attached to this
book.
Setting Up jOOQ
To start, you’ll need to reckon with the particulars of the RDBMS you’re
going to be working with. A lot of the beauty of jOOQ is how it allows you
to use pretty much any database with tons of convenience. Features that
aren’t available in your chosen database can be emulated transparently by
the jOOQ API. For this book, I’ll be doing most of the demonstrations with
MySQL, with some detours into the some other popular databases every
now and then.
20
Chapter 2 Getting Started with jOOQ
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-driver-version}</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.15.1</version>
</dependency>
21
Chapter 2 Getting Started with jOOQ
Simple enough, yes? Bueno. When you’ve paid for the commercial
version however, things get a bit more manual. Here’s how.
• Sources
• Compiled JARs
• Helper scripts
• maven-deploy
• maven-install
Both helpers do the same thing with different targets: build and install
the jOOQ jars into repositories. maven-deploy will set up the jOOQ JARs
in a remote repository, so reach for that to set up the JAR in a central
2
Commercial = Express, Professional, and Enterprise licenses
22
Chapter 2 Getting Started with jOOQ
<dependency>
<groupId>org.jooq.pro</groupId>
<artifactId>jooq</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>org.jooq.pro</groupId>
<artifactId>jooq-meta</artifactId>
<version>3.15.1</version>
</dependency>
<dependency>
<groupId>org.jooq.pro</groupId>
<artifactId>jooq-codegen</artifactId>
<version>3.15.1</version>
</dependency>
It’s the same artifactId as the free version, but with a different
groupId:org.jooq.pro. You can use a trial license for the commercial
version of jOOQ by using org.jooq.trial for the groupId. The open
source version is just as functional for many use cases, but with limited
database vendor support and fewer features.3 Also noteworthy that jOOQ’s
JDK support starts from JDK 6, all the way up to the latest (17, as at the
time of this writing). The older JDK versions are supported only with the
paid version.
3
www.jooq.org/download/#feature-comparison
23
Chapter 2 Getting Started with jOOQ
Tip The trial version of the commercial jOOQ distribution will print
a message indicating that yes, you are indeed on a trial version of
the commercial jOOQ distribution. But rejoice, oh ye trial version
users, for jOOQ is effective as it is generous: set the org.jooq.
no-logo=true JVM property to disable the trial license warning
message.
And that’s it! jOOQ is set up in your project. Now to do stuff with it…
24
Chapter 2 Getting Started with jOOQ
Remember DSL and DSLContext are the primary entry points into
the jOOQ ecosystem.
25
Chapter 2 Getting Started with jOOQ
4
You can control this behavior by configuring the ANSI_QUOTES option
for MySQL (https://dev.mysql.com/doc/refman/8.0/en/sql-mode.
html#sqlmode_ansi_quotes).
26
Chapter 2 Getting Started with jOOQ
If you don’t specify any dialect, you’ll get the default SQLDialect.
DEFAULT and then via con Dios!5
Caution For your own peace of mind, go ahead and configure the
org.jooq.conf.Settings.backslashEscaping property on
your Settings object. MySQL and some versions of PostgreSQL
support non-standard escape characters that can cause you a lot of
grief when you least expect it. This property lets jOOQ properly handle
this “feature” from MySQL.
5
Lukas: If you provide DSL.using(connection), then jOOQ will try to “guess”
the appropriate SQLDialect from the JDBC DatabaseMetaData – https://docs.
oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html
27
Chapter 2 Getting Started with jOOQ
1. org.jooq.Query
28
Chapter 2 Getting Started with jOOQ
2. org.jooq.ResultQuery
6
Lukas: It’s time to deprecate that ancient feature of the mutable ResultQuery!
This won’t be available in the future anymore. In the future, only
ResultQuery::fetch and similar methods should be used.
29
Chapter 2 Getting Started with jOOQ
30
Chapter 2 Getting Started with jOOQ
4. org.jooq.Record
31
Chapter 2 Getting Started with jOOQ
• org.jooq.UpdatableRecord
• org.jooq.TableRecord
• org.jooq.RecordMapper
32
Exploring the Variety of Random
Documents with Different Content
VII.
*****
— Vai niin!
— Miksi niin?
*****
— Niin.
— Fênitshka … Feodôsia.
— Nikolâjewna.
— Ei minun mielestäni.
— Isä soittaa.
*****
— Kuinka niin?