Skip to content

Commit b0de52b

Browse files
committed
Document Hibernate ORM dialect configuration
1 parent d0ea5ca commit b0de52b

File tree

1 file changed

+90
-2
lines changed

1 file changed

+90
-2
lines changed

docs/src/main/asciidoc/hibernate-orm.adoc

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ For a list of the items that you can set in `{config-file}`, see xref:hibernate-
102102

103103
An `EntityManagerFactory` will be created based on the Quarkus `datasource` configuration as long as the Hibernate ORM extension is listed among your project dependencies.
104104

105-
Unless you set one explicitly, the dialect will be selected based on the JDBC driver.
105+
The dialect will be selected and configured automatically based on your datasource;
106+
you may want to <<hibernate-dialect,configure it to more precisely match your database>>.
106107

107108
You can then happily inject your `EntityManager`:
108109

@@ -165,6 +166,93 @@ WARNING: Make sure to wrap methods modifying your database (e.g. `entity.persist
165166
CDI bean method `@Transactional` will do that for you and make that method a transaction boundary. We recommend doing
166167
so at your application entry point boundaries like your REST endpoint controllers.
167168

169+
[[hibernate-dialect]]
170+
=== Dialect
171+
172+
[[hibernate-dialect-supported-databases]]
173+
==== Supported databases
174+
175+
For xref:datasource.adoc#default-datasource[supported databases],
176+
the link:{orm-doc-url-prefix}##database-dialect[Hibernate ORM dialect]
177+
does not need to be set explicitly:
178+
it is selected automatically based on the datasource.
179+
180+
By default, the dialect is configured to target the minimum supported version of the database.
181+
182+
In order for Hibernate ORM to generate more efficient SQL,
183+
to avoid workarounds and to take advantage of more database features,
184+
you can set the database version explicitly:
185+
186+
[source,properties]
187+
.`{config-file}` with an explicit `db-version`
188+
----
189+
quarkus.datasource.db-kind = postgresql
190+
quarkus.datasource.db-version = 14.0 <1>
191+
quarkus.datasource.username = hibernate
192+
quarkus.datasource.password = hibernate
193+
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:5432/hibernate_db
194+
----
195+
<1> Set the database version. The Hibernate ORM dialect will target that version.
196+
197+
As a rule, the version set here should be as high as possible,
198+
but must be lower than or equal to the version of any database your application will connect to.
199+
200+
[NOTE]
201+
====
202+
When a version is set explicitly,
203+
Quarkus will try to check this version against the actual database version on startup,
204+
leading to a startup failure when the actual version is lower.
205+
206+
This is because Hibernate ORM may generate SQL that is invalid
207+
for versions of the database older than what is configured,
208+
which would lead to runtime exceptions.
209+
210+
If the database cannot be reached, a warning will be logged but startup will proceed.
211+
====
212+
213+
[[hibernate-dialect-other-databases]]
214+
==== Other databases
215+
216+
If xref:datasource.adoc#other-databases[your database does not have a corresponding Quarkus extension],
217+
or if the defaults do not match your needs for some reason,
218+
you will need to set the link:{orm-doc-url-prefix}##database-dialect[Hibernate ORM dialect] explicitly:
219+
220+
[source,properties]
221+
.`{config-file}` with an explicit `dialect`
222+
----
223+
quarkus.datasource.db-kind = postgresql
224+
quarkus.datasource.username = hibernate
225+
quarkus.datasource.password = hibernate
226+
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:26257/hibernate_db
227+
228+
quarkus.hibernate-orm.dialect=org.hibernate.dialect.CockroachDialect <1>
229+
----
230+
<1> Set the Hibernate ORM dialect.
231+
232+
[WARNING]
233+
====
234+
In that case, keep in mind that the JDBC driver or Hibernate ORM dialect
235+
may not work properly in GraalVM native executables.
236+
====
237+
238+
As with <<hibernate-dialect-supported-databases,supported databases>>,
239+
you can configure the DB version explicitly to get the most out of Hibernate ORM:
240+
241+
[source,properties]
242+
.`{config-file}` with an explicit `dialect` and `db-version`
243+
----
244+
quarkus.datasource.db-kind = postgresql
245+
quarkus.datasource.db-version = 22.2 <1>
246+
quarkus.datasource.username = hibernate
247+
quarkus.datasource.password = hibernate
248+
quarkus.datasource.jdbc.url = jdbc:postgresql://localhost:26257/hibernate_db
249+
250+
quarkus.hibernate-orm.dialect=org.hibernate.dialect.CockroachDialect <2>
251+
----
252+
<1> Set the database version. The Hibernate ORM dialect will target that version.
253+
Since we're targeting CockroachDB here, we're passing the CockroachDB version, not the PostgreSQL version.
254+
<2> Set the Hibernate ORM dialect.
255+
168256
[[hibernate-configuration-properties]]
169257
=== Hibernate ORM configuration properties
170258

@@ -417,7 +505,7 @@ difference is that you would specify your Hibernate ORM configuration in `META-I
417505
418506
<properties>
419507
<!-- Connection specific -->
420-
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL95Dialect"/>
508+
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
421509
422510
<property name="hibernate.show_sql" value="true"/>
423511
<property name="hibernate.format_sql" value="true"/>

0 commit comments

Comments
 (0)