Skip to content

Commit 9f714b7

Browse files
committed
Add more info on Hibernate ORM validation modes to the documentation
1 parent d2361f4 commit 9f714b7

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-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
@@ -1510,3 +1510,81 @@ Format mappers *must* have both `@PersistenceUnitExtension` and either `@JsonFor
15101510
15111511
Having multiple JSON (or XML) format mappers registered for the same persistence unit will result in an exception, because of the ambiguity.
15121512
====
1513+
1514+
[[validator_integration]]
1515+
== Validation modes and Hibernate Validator integration
1516+
1517+
Hibernate Validator integration into Hibernate ORM opens up the following capabilities:
1518+
1519+
- performing entity validation on lifecycle events
1520+
- applying constraint information from entities to DDL
1521+
1522+
From Quarkus's perspective, this is controlled by the <<quarkus-hibernate-orm_quarkus-hibernate-orm-validation-mode,`quarkus.hibernate-orm.validation.mode` configuration property>>.
1523+
The available validation modes are:
1524+
1525+
- `auto` -- the default option; works the same as `callback` and `ddl` enabled simultaneously
1526+
when the Jakarta Validation provider is available (i.e. the Hibernate Validator extension is enabled),
1527+
and as `none` if no provider is available.
1528+
- `callback` -- Hibernate Validator will perform the lifecycle event validation.
1529+
- `ddl` -- Jakarta Validation constraints will be considered for the DDL operations
1530+
- `none` -- Jakarta Validation integration will be disabled.
1531+
1532+
While all constraints with corresponding Jakarta Validation rules will apply during the `callback` validation,
1533+
in the `ddl` mode, only a subset of constraints is used to influence the DDL.
1534+
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.
1535+
1536+
Let's consider having a simple entity with some constraints applied to its properties:
1537+
1538+
[source,java]
1539+
----
1540+
import jakarta.validation.constraints.NotEmpty;
1541+
import jakarta.validation.constraints.NotNull;
1542+
import jakarta.validation.constraints.Size;
1543+
1544+
@Entity
1545+
public class MyEntity {
1546+
@Id
1547+
public long id;
1548+
1549+
@NotNull
1550+
@NotEmpty
1551+
@Size(max = 50)
1552+
public String name;
1553+
1554+
public String value;
1555+
}
1556+
----
1557+
1558+
With the `ddl` mode enabled, the resulting schema would be expected to have the following constraints:
1559+
[source,sql]
1560+
----
1561+
create table myentity
1562+
(
1563+
id bigint not null primary key,
1564+
name varchar(50) not null, // <1>
1565+
value varchar(255), // <2>
1566+
);
1567+
----
1568+
<1> The `name` column has a `not null` constraint because of the `@NotNull` constraint,
1569+
and the length of the values is limited to `50` because of the `@Size(max=50)` constraint.
1570+
+
1571+
<2> Since the `value` property in the entity has no constraints, there are no additional constraints in the DDL,
1572+
and the `255` length limit comes from the default `jakarta.persistence.Column#lenght()`.
1573+
1574+
With the `callback` mode, expect getting a `jakarta.validation.ConstraintViolationException` thrown:
1575+
1576+
[source,java]
1577+
----
1578+
try {
1579+
MyEntity entity = new MyEntity();
1580+
entity.setName(veryLongName);
1581+
em.persist(entity);
1582+
em.flush();
1583+
} catch (ConstraintViolationException exception) {
1584+
// handle the constraint violations somehow
1585+
}
1586+
----
1587+
1588+
Since Quarkus has built-in exception mappers for `jakarta.validation.ConstraintViolationException`,
1589+
explicitly handling these exceptions might be redundant. See the xref:validation.adoc#rest-end-point-validation[REST end point validation]
1590+
section of the Hibernate Validator guide for more details.

0 commit comments

Comments
 (0)