Reinstate support for jOOQ as it now supports Jakarta EE 9

Closes gh-29271
This commit is contained in:
Andy Wilkinson
2022-01-05 15:46:54 +00:00
parent ccc924b908
commit c2e86096cd
46 changed files with 2201 additions and 2 deletions

View File

@@ -230,6 +230,11 @@ boot-features-spring-data-jpa-repositories=features.sql.jpa-and-spring-data.repo
boot-features-creating-and-dropping-jpa-databases=features.sql.jpa-and-spring-data.creating-and-dropping
boot-features-jpa-in-web-environment=features.sql.jpa-and-spring-data.open-entity-manager-in-view
boot-features-data-jdbc=features.sql.jdbc
boot-features-jooq=features.sql.jooq
boot-features-jooq-codegen=features.sql.jooq.codegen
boot-features-jooq-dslcontext=features.sql.jooq.dslcontext
boot-features-jooq-sqldialect=features.sql.jooq.sqldialect
boot-features-jooq-customizing=features.sql.jooq.customizing
boot-features-r2dbc=features.sql.r2dbc
boot-features-r2dbc-embedded-database=features.sql.r2dbc.embedded
boot-features-r2dbc-using-database-client=features.sql.r2dbc.using-database-client
@@ -333,6 +338,7 @@ boot-features-testing-spring-boot-applications-testing-autoconfigured-cassandra-
boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jpa
boot-features-testing-spring-boot-applications-testing-autoconfigured-jdbc-test=features.testing.spring-boot-applications.autoconfigured-jdbc
boot-features-testing-spring-boot-applications-testing-autoconfigured-data-jdbc-test=features.testing.spring-boot-applications.autoconfigured-spring-data-jdbc
boot-features-testing-spring-boot-applications-testing-autoconfigured-jooq-test=features.testing.spring-boot-applications.autoconfigured-jooq
boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test=features.testing.spring-boot-applications.autoconfigured-spring-data-mongodb
boot-features-testing-spring-boot-applications-testing-autoconfigured-neo4j-test=features.testing.spring-boot-applications.autoconfigured-spring-data-neo4j
boot-features-testing-spring-boot-applications-testing-autoconfigured-redis-test=features.testing.spring-boot-applications.autoconfigured-spring-data-redis
@@ -637,6 +643,7 @@ howto-use-spring-data-jpa--and-mongo-repositories=howto.data-access.use-spring-d
howto-use-customize-spring-datas-web-support=howto.data-access.customize-spring-data-web-support
howto-use-exposing-spring-data-repositories-rest-endpoint=howto.data-access.exposing-spring-data-repositories-as-rest
howto-configure-a-component-that-is-used-by-JPA=howto.data-access.configure-a-component-that-is-used-by-jpa
howto-configure-jOOQ-with-multiple-datasources=howto.data-access.configure-jooq-with-multiple-datasources
howto-database-initialization=howto.data-initialization
howto-initialize-a-database-using-jpa=howto.data-initialization.using-jpa
howto-initialize-a-database-using-hibernate=howto.data-initialization.using-hibernate
@@ -909,6 +916,11 @@ features.sql.jpa-and-spring-data.open-entity-manager-in-view=data.sql.jpa-and-sp
features.sql.jdbc=data.sql.jdbc
features.sql.h2-web-console=data.sql.h2-web-console
features.sql.h2-web-console.custom-path=data.sql.h2-web-console.custom-path
features.sql.jooq=data.sql.jooq
features.sql.jooq.codegen=data.sql.jooq.codegen
features.sql.jooq.dslcontext=data.sql.jooq.dslcontext
features.sql.jooq.sqldialect=data.sql.jooq.sqldialect
features.sql.jooq.customizing=data.sql.jooq.customizing
features.sql.r2dbc=data.sql.r2dbc
features.sql.r2dbc.embedded=data.sql.r2dbc.embedded
features.sql.r2dbc.using-database-client=data.sql.r2dbc.using-database-client

View File

@@ -98,6 +98,7 @@
:gradle-docs: https://docs.gradle.org/current/userguide
:hibernate-docs: https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html
:java-api: https://docs.oracle.com/javase/8/docs/api
:jooq-docs: https://www.jooq.org/doc/{jooq-version}/manual-single-page
:junit5-docs: https://junit.org/junit5/docs/current/user-guide
:kotlin-docs: https://kotlinlang.org/docs/reference/
:lettuce-docs: https://lettuce.io/core/{lettuce-version}/reference/index.html

View File

@@ -315,6 +315,90 @@ TIP: For complete details of Spring Data JDBC, see the {spring-data-jdbc-docs}[r
[[data.sql.jooq]]
=== Using jOOQ
jOOQ Object Oriented Querying (https://www.jooq.org/[jOOQ]) is a popular product from https://www.datageekery.com/[Data Geekery] which generates Java code from your database and lets you build type-safe SQL queries through its fluent API.
Both the commercial and open source editions can be used with Spring Boot.
[[data.sql.jooq.codegen]]
==== Code Generation
In order to use jOOQ type-safe queries, you need to generate Java classes from your database schema.
You can follow the instructions in the {jooq-docs}/#jooq-in-7-steps-step3[jOOQ user manual].
If you use the `jooq-codegen-maven` plugin and you also use the `spring-boot-starter-parent` "`parent POM`", you can safely omit the plugin's `<version>` tag.
You can also use Spring Boot-defined version variables (such as `h2.version`) to declare the plugin's database dependency.
The following listing shows an example:
[source,xml,indent=0,subs="verbatim"]
----
<plugin>
<groupId>org.jooq</groupId>
<artifactId>jooq-codegen-maven</artifactId>
<executions>
...
</executions>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
</dependencies>
<configuration>
<jdbc>
<driver>org.h2.Driver</driver>
<url>jdbc:h2:~/yourdatabase</url>
</jdbc>
<generator>
...
</generator>
</configuration>
</plugin>
----
[[data.sql.jooq.dslcontext]]
==== Using DSLContext
The fluent API offered by jOOQ is initiated through the `org.jooq.DSLContext` interface.
Spring Boot auto-configures a `DSLContext` as a Spring Bean and connects it to your application `DataSource`.
To use the `DSLContext`, you can inject it, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/data/sql/jooq/dslcontext/MyBean.java[tag=!method]
----
TIP: The jOOQ manual tends to use a variable named `create` to hold the `DSLContext`.
You can then use the `DSLContext` to construct your queries, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/data/sql/jooq/dslcontext/MyBean.java[tag=method]
----
[[data.sql.jooq.sqldialect]]
==== jOOQ SQL Dialect
Unless the configprop:spring.jooq.sql-dialect[] property has been configured, Spring Boot determines the SQL dialect to use for your datasource.
If Spring Boot could not detect the dialect, it uses `DEFAULT`.
NOTE: Spring Boot can only auto-configure dialects supported by the open source version of jOOQ.
[[data.sql.jooq.customizing]]
==== Customizing jOOQ
More advanced customizations can be achieved by defining your own `DefaultConfigurationCustomizer` bean that will be invoked prior to creating the `org.jooq.Configuration` `@Bean`.
This takes precedence to anything that is applied by the auto-configuration.
You can also create your own `org.jooq.Configuration` `@Bean` if you want to take complete control of the jOOQ configuration.
[[data.sql.r2dbc]]
=== Using R2DBC
The Reactive Relational Database Connectivity (https://r2dbc.io[R2DBC]) project brings reactive programming APIs to relational databases.

View File

@@ -255,7 +255,7 @@ Spring Boot includes the following pre-defined logging groups that can be used o
| `org.springframework.core.codec`, `org.springframework.http`, `org.springframework.web`, `org.springframework.boot.actuate.endpoint.web`, `org.springframework.boot.web.servlet.ServletContextInitializerBeans`
| sql
| `org.springframework.jdbc.core`, `org.hibernate.SQL`
| `org.springframework.jdbc.core`, `org.hibernate.SQL`, `org.jooq.tools.LoggerListener`
|===

View File

@@ -553,6 +553,30 @@ If you prefer your test to run against a real database, you can use the `@AutoCo
[[features.testing.spring-boot-applications.autoconfigured-jooq]]
==== Auto-configured jOOQ Tests
You can use `@JooqTest` in a similar fashion as `@JdbcTest` but for jOOQ-related tests.
As jOOQ relies heavily on a Java-based schema that corresponds with the database schema, the existing `DataSource` is used.
If you want to replace it with an in-memory database, you can use `@AutoConfigureTestDatabase` to override those settings.
(For more about using jOOQ with Spring Boot, see "<<data#data.sql.jooq>>", earlier in this chapter.)
Regular `@Component` and `@ConfigurationProperties` beans are not scanned when the `@JooqTest` annotation is used.
`@EnableConfigurationProperties` can be used to include `@ConfigurationProperties` beans.
TIP: A list of the auto-configurations that are enabled by `@JooqTest` can be <<test-auto-configuration#test-auto-configuration,found in the appendix>>.
`@JooqTest` configures a `DSLContext`.
The following example shows the `@JooqTest` annotation in use:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/features/testing/springbootapplications/autoconfiguredjooq/MyJooqTests.java[]
----
JOOQ tests are transactional and roll back at the end of each test by default.
If that is not what you want, you can disable transaction management for a test or for the whole test class as <<features#features.testing.spring-boot-applications.autoconfigured-jdbc,shown in the JDBC example>>.
[[features.testing.spring-boot-applications.autoconfigured-spring-data-mongodb]]
==== Auto-configured Data MongoDB Tests
You can use `@DataMongoTest` to test MongoDB applications.

View File

@@ -394,3 +394,12 @@ For example, if you use Hibernate Search with Elasticsearch as its index manager
----
include::{docs-java}/howto/dataaccess/configureacomponentthatisusedbyjpa/ElasticsearchEntityManagerFactoryDependsOnPostProcessor.java[]
----
[[howto.data-access.configure-jooq-with-multiple-datasources]]
=== Configure jOOQ with Two DataSources
If you need to use jOOQ with multiple data sources, you should create your own `DSLContext` for each one.
See {spring-boot-autoconfigure-module-code}/jooq/JooqAutoConfiguration.java[JooqAutoConfiguration] for more details.
TIP: In particular, `JooqExceptionTranslator` and `SpringTransactionProvider` can be reused to provide similar features to what the auto-configuration does with a single `DataSource`.

View File

@@ -209,6 +209,7 @@ To have other beans be detected, register an implementation of `DatabaseInitiali
Spring Boot will automatically detect beans of the following types that depends upon database initialization:
- `AbstractEntityManagerFactoryBean` (unless configprop:spring.jpa.defer-datasource-initialization[] is set to `true`)
- `DSLContext` (jOOQ)
- `EntityManagerFactory` (unless configprop:spring.jpa.defer-datasource-initialization[] is set to `true`)
- `JdbcOperations`
- `NamedParameterJdbcOperations`