Skip to content

Commit 262db40

Browse files
committed
Add more info on Hibernate ORM validation modes to the documentation
1 parent f97f6df commit 262db40

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

docs/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3219,6 +3219,17 @@
32193219
<versionString>${hibernate-search.version}</versionString>
32203220
</configuration>
32213221
</execution>
3222+
<execution>
3223+
<id>parse-version-hibernate-validator</id>
3224+
<goals>
3225+
<goal>parse-version</goal>
3226+
</goals>
3227+
<phase>validate</phase>
3228+
<configuration>
3229+
<propertyPrefix>hibernate-validator</propertyPrefix>
3230+
<versionString>${hibernate-validator.version}</versionString>
3231+
</configuration>
3232+
</execution>
32223233
</executions>
32233234
</plugin>
32243235
<plugin>

docs/src/main/asciidoc/_attributes.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
// overridden to the full version, at least when building locally.
3030
:hibernate-orm-version-major-minor: ${hibernate-orm.majorVersion}.${hibernate-orm.minorVersion}
3131
:hibernate-search-version-major-minor: ${hibernate-search.majorVersion}.${hibernate-search.minorVersion}
32+
:hibernate-validator-version-major-minor: ${hibernate-validator.majorVersion}.${hibernate-validator.minorVersion}
3233
// .
3334
:quarkus-home-url: ${quarkus-home-url}
3435
:quarkus-org-url: https://github.com/quarkusio
@@ -54,6 +55,7 @@
5455
:hibernate-orm-docs-url: https://docs.jboss.org/hibernate/orm/{hibernate-orm-version-major-minor}/userguide/html_single/Hibernate_User_Guide.html
5556
:hibernate-orm-dialect-docs-url: https://docs.jboss.org/hibernate/orm/{hibernate-orm-version-major-minor}/dialect/dialect.html
5657
:hibernate-search-docs-url: https://docs.jboss.org/hibernate/search/{hibernate-search-version-major-minor}/reference/en-US/html_single/
58+
:hibernate-validator-docs-url: https://docs.jboss.org/hibernate/validator/{hibernate-validator-version-major-minor}/reference/en-US/html_single/
5759
// .
5860
:amazon-services-guide: https://docs.quarkiverse.io/quarkus-amazon-services/dev/index.html
5961
:config-consul-guide: https://docs.quarkiverse.io/quarkus-config-extensions/dev/consul.html

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,3 +1569,81 @@ Format mappers *must* have both `@PersistenceUnitExtension` and either `@JsonFor
15691569
15701570
Having multiple JSON (or XML) format mappers registered for the same persistence unit will result in an exception, because of the ambiguity.
15711571
====
1572+
1573+
[[validator_integration]]
1574+
== Validation modes and Hibernate Validator integration
1575+
1576+
Hibernate Validator integration into Hibernate ORM opens up the following capabilities:
1577+
1578+
- performing entity validation on lifecycle events
1579+
- applying constraint information from entities to DDL
1580+
1581+
From Quarkus's perspective, this is controlled by the <<quarkus-hibernate-orm_quarkus-hibernate-orm-validation-mode,`quarkus.hibernate-orm.validation.mode` configuration property>>.
1582+
The available validation modes are:
1583+
1584+
- `auto` -- the default option; works the same as `callback` and `ddl` enabled simultaneously
1585+
when the application uses `quarkus-hibernate-validator`,
1586+
and as `none` otherwise.
1587+
- `callback` -- Hibernate Validator will perform the lifecycle event validation.
1588+
- `ddl` -- Hibernate Validator constraints will be considered for the <<dev-mode,DDL operations>>
1589+
- `none` -- Hibernate Validator integration will be disabled.
1590+
1591+
While all constraints with corresponding Jakarta Validation rules will apply during the `callback` validation,
1592+
in the `ddl` mode, only a subset of constraints is used to influence the DDL.
1593+
In the Hibernate Validator documentation, you can find link:{hibernate-validator-docs-url}#section-builtin-constraints[the list of available constraints] and their impact on the DDL generation.
1594+
1595+
Let's consider having a simple entity with some constraints applied to its properties:
1596+
1597+
[source,java]
1598+
----
1599+
import jakarta.validation.constraints.NotEmpty;
1600+
import jakarta.validation.constraints.NotNull;
1601+
import jakarta.validation.constraints.Size;
1602+
1603+
@Entity
1604+
public class MyEntity {
1605+
@Id
1606+
public long id;
1607+
1608+
@NotNull
1609+
@NotEmpty
1610+
@Size(max = 50)
1611+
public String name;
1612+
1613+
public String value;
1614+
}
1615+
----
1616+
1617+
With the `ddl` mode enabled, the resulting schema would be expected to have the following constraints:
1618+
[source,sql]
1619+
----
1620+
create table myentity
1621+
(
1622+
id bigint not null primary key,
1623+
name varchar(50) not null, // <1>
1624+
value varchar(255), // <2>
1625+
);
1626+
----
1627+
<1> The `name` column has a `not null` constraint because of the `@NotNull` constraint,
1628+
and the length of the values is limited to `50` because of the `@Size(max=50)` constraint.
1629+
+
1630+
<2> Since the `value` property in the entity has no constraints, there are no additional constraints in the DDL,
1631+
and the `255` length limit comes from the default `jakarta.persistence.Column#lenght()`.
1632+
1633+
With the `callback` mode, expect getting a `jakarta.validation.ConstraintViolationException` thrown:
1634+
1635+
[source,java]
1636+
----
1637+
try {
1638+
MyEntity entity = new MyEntity();
1639+
entity.setName(veryLongName);
1640+
em.persist(entity);
1641+
em.flush();
1642+
} catch (ConstraintViolationException exception) {
1643+
// handle the constraint violations somehow
1644+
}
1645+
----
1646+
1647+
Since Quarkus has built-in exception mappers for `jakarta.validation.ConstraintViolationException`,
1648+
explicitly handling these exceptions might be redundant. See the xref:validation.adoc#rest-end-point-validation[REST end point validation]
1649+
section of the Hibernate Validator guide for more details.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,8 @@ consider xref:hibernate-reactive-panache.adoc#transactions[using `@WithTransacti
333333
The xref:hibernate-reactive-panache.adoc[Hibernate Reactive with Panache] extension facilitates the usage of Hibernate Reactive
334334
by providing active record style entities (and repositories) and focuses on making your entities trivial and fun to write in Quarkus.
335335

336+
== Validation modes and Hibernate Validator integration
337+
338+
To find out more on how the <<quarkus-hibernate-orm_quarkus-hibernate-orm-validation-mode,`quarkus.hibernate-orm.validation.mode` configuration property>>.
339+
influence your Hibernate Reactive application see the xref:hibernate-orm.adoc#validator_integration[corresponding Hibernate ORM guide],
340+
as these modes work the same in both cases.

0 commit comments

Comments
 (0)