Hibernate Validator is the reference implementation for the Bean Validation specification. For more information, see Hibernate Validator on hibernate.org.

We just published Hibernate Validator 9.1.0.Alpha2, the next release of the new 9.1 series of Hibernate Validator.

This series primarily aims to improve performance. It also contains a few improvements, fixes and dependency updates.

As this release includes significant changes to the processed bean tracking, path implementation and related areas, we encourage users to give it a try and report any findings, especially if there are non-trivial validation scenarios.

What’s new

Performance improvements

In this version, we kept improving the performance. We also finished preparing a more detailed report on performance improvements and will publish it soon, so stay tuned if you are interested in the details and numbers.

Extended validation path

This version introduces another extension of the jakarta.validation.Path: org.hibernate.validator.path.RandomAccessPath. There are scenarios where the first couple of nodes have to be inspected to determine how to process the constraint violation. For cases when the path is represented by an array or some other collection that allows easy random access to the nodes, it would be simple enough to expose the access to the nodes by index:

Example 1. Random access to the path nodes
Path path = constraintViolation.getPropertyPath();
if ( path instanceof org.hibernate.validator.path.RandomAccessPath hvPath ) {
    Node rootNode = hvPath.getRootNode();
    // ...
    int index = ...
    Node someNode = hvPath.getNode(index);
    // ...
    for(int i; i < hvPath.length(); i++) {
        hvPath.getNode(i);
    }
}

Constraint initialization shared data

Constraint initialization shared data opens up a way for constraint validators to access a shared instance within the initialize(..). This can be used to cache and reuse elements required to construct a constraint validator. For example, internally, this mechanism is used by the pattern constraint validator to reuse the java.util.regex.Pattern instances

Example 2. Accessing the constraint validator’s lazy initialization shared data in a constraint validator
public class ParsableDateTimeFormatValidator
    implements HibernateConstraintValidator<ParsableDateTimeFormat, String> { (1)

    private DateTimeFormatter formatter;

    @Override
    public void initialize(ConstraintDescriptor<ParsableDateTimeFormat> constraintDescriptor,
            HibernateConstraintValidatorInitializationContext initializationContext) {
        formatter = initializationContext.getSharedData( DateTimeFormatterCache.class, DateTimeFormatterCache::new ) (2)
                .get( constraintDescriptor.getAnnotation().dateFormat() ); (3)
    }

    @Override
    public boolean isValid(String dateTime, ConstraintValidatorContext constraintContext) {
        if ( dateTime == null ) {
            return true;
        }

        try {
            formatter.parse( dateTime );
        }
        catch (DateTimeParseException e) {
            return false;
        }
        return true;
    }

    private static class DateTimeFormatterCache { (4)
        private final Map<String, DateTimeFormatter> cache = new ConcurrentHashMap<>();

        DateTimeFormatter get(String format) {
            return cache.computeIfAbsent( format, DateTimeFormatter::ofPattern );
        }
    }
}
1 Implement the Hibernate Validator HibernateConstraintValidator extension to have access to the initialization context.
2 Retrieve the shared data from the initialization context, providing the supplier that will be executed if the DateTimeFormatterCache is not yet available in the current initialization context.
3 Perform some actions with the shared data instance.
4 A simple wrapper around the map to cache the formatters. Compared to the use of a static field cache, using the shared data has the benefit that it is tied to the initialization context and will be garbage collected along with it.

See the corresponding section on constraint initialization shared data to find out more.

Other improvements and bug fixes

  • HV-2004: java.util.regex.Pattern cache for @Pattern constraints

Please see the release notes for a complete list of changes since the previous releases.

How to get this release

Hibernate Validator 9 targets the Jakarta EE 11.

All details are available and up to date on the dedicated page on hibernate.org.

Getting started, migrating

For new applications, please refer to the getting started guide:

For existing applications, Hibernate Validator 9.1 is a drop-in replacement for 9.0. Information about deprecated configuration and API is included in the migration guide.

Feedback, issues, ideas?

To get in touch, use the usual channels:


Back to top