Skip to content

Commit

Permalink
HV-2009 Use "Jakarta Persistence"
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Jul 15, 2024
1 parent 4c62854 commit a6a780f
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 20 deletions.
1 change: 1 addition & 0 deletions documentation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@

<jdkVersion>${java-version.main.release}</jdkVersion>
<compilerPluginVersion>${version.compiler.plugin}</compilerPluginVersion>
<versionJakartaPersistence>${version.jakarta.persistence}</versionJakartaPersistence>

<!-- URLs -->
<javaApiDocsUrl>${java.api-docs.base-url}/</javaApiDocsUrl>
Expand Down
6 changes: 3 additions & 3 deletions documentation/src/main/asciidoc/ch09.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ include::{sourcedir}/org/hibernate/validator/referenceguide/chapter09/Bootstrapp
==== `TraversableResolver`

In some cases the validation engine should not access the state of a bean property.
The most obvious example for that is a lazily loaded property or association of a JPA entity.
The most obvious example for that is a lazily loaded property or association of a Jakarta Persistence entity.
Validating this lazy property or association would mean that its state would have to be accessed,
triggering a load from the database.

Expand All @@ -174,11 +174,11 @@ include::{sourcedir}/org/hibernate/validator/referenceguide/chapter09/Bootstrapp
====

If no specific traversable resolver has been configured, the default behavior is to consider all properties as reachable and cascadable.
When using Hibernate Validator together with a JPA 2 provider such as Hibernate ORM, only those properties will be considered reachable
When using Hibernate Validator together with a Jakarta Persistence {versionJakartaPersistence} provider such as Hibernate ORM, only those properties will be considered reachable
which already have been loaded by the persistence provider and all properties will be considered cascadable.

By default, the traversable resolver calls are cached per validation call.
This is especially important in a JPA environment where calling `isReachable()` has a significant cost.
This is especially important in a Jakarta Persistence environment where calling `isReachable()` has a significant cost.

This caching adds some overhead.
In the case your custom traversable resolver is very fast, it might be better to consider turning off the cache.
Expand Down
5 changes: 3 additions & 2 deletions documentation/src/main/asciidoc/ch11.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ configuration in _hibernate.cfg.xml_:
----
====

==== JPA
==== Jakarta Persistence

If you are using JPA 2 and Hibernate Validator is in the classpath, the JPA 2 specification requires
If you are using Jakarta Persistence {versionJakartaPersistence} and Hibernate Validator is in the classpath,
the Jakarta Persistence {versionJakartaPersistence} specification requires
that Jakarta Validation gets enabled. The properties `jakarta.persistence.validation.group.pre-persist`,
`jakarta.persistence.validation.group.pre-update` and `jakarta.persistence.validation.group.pre-remove` as
described in <<validator-checkconstraints-orm-hibernateevent>> can in this case be configured in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
* An implementation of {@code TraversableResolver} which is aware of JPA 2 and utilizes {@code PersistenceUtil} to
* An implementation of {@code TraversableResolver} which is aware of Jakarta Persistence and utilizes {@code PersistenceUtil} to
* query the reachability of a property.
* This resolver will be automatically enabled if JPA 2 is on the classpath and the default {@code TraversableResolver} is
* This resolver will be automatically enabled if Jakarta Persistence is on the classpath and the default {@code TraversableResolver} is
* used.
* <p>
* This class needs to be public as it's instantiated via a privileged action that is not in this package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class TraversableResolvers {
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() );

/**
* Class to load to check whether JPA is on the classpath.
* Class to load to check whether Jakarta Persistence is on the classpath.
*/
private static final String PERSISTENCE_CLASS_NAME = "jakarta.persistence.Persistence";

Expand All @@ -34,7 +34,7 @@ public class TraversableResolvers {
private static final String PERSISTENCE_UTIL_METHOD = "getPersistenceUtil";

/**
* Class to instantiate in case JPA 2 is on the classpath.
* Class to instantiate in case Jakarta Persistence is on the classpath.
*/
private static final String JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME = "org.hibernate.validator.internal.engine.resolver.JPATraversableResolver";

Expand All @@ -44,7 +44,7 @@ private TraversableResolvers() {
/**
* Initializes and returns the default {@link TraversableResolver} depending on the environment.
* <p>
* If JPA 2 is present in the classpath, a {@link JPATraversableResolver} instance is returned.
* If Jakarta Persistence is present in the classpath, a {@link JPATraversableResolver} instance is returned.
* <p>
* Otherwise, it returns an instance of the default {@link TraverseAllTraversableResolver}.
*/
Expand All @@ -56,7 +56,7 @@ public static TraversableResolver getDefault() {
}
catch (ValidationException e) {
LOG.debugf(
"Cannot find %s on classpath. Assuming non JPA 2 environment. All properties will per default be traversable.",
"Cannot find %s on classpath. Assuming non Jakarta Persistence environment. All properties will per default be traversable.",
PERSISTENCE_CLASS_NAME
);
return getTraverseAllTraversableResolver();
Expand All @@ -81,15 +81,15 @@ public static TraversableResolver getDefault() {
}
catch (Exception e) {
LOG.debugf(
"Unable to invoke %s.%s. Inconsistent JPA environment. All properties will per default be traversable.",
"Unable to invoke %s.%s. Inconsistent Jakarta Persistence environment. All properties will per default be traversable.",
PERSISTENCE_CLASS_NAME,
PERSISTENCE_UTIL_METHOD
);
return getTraverseAllTraversableResolver();
}

LOG.debugf(
"Found %s on classpath containing '%s'. Assuming JPA 2 environment. Trying to instantiate JPA aware TraversableResolver",
"Found %s on classpath containing '%s'. Assuming Jakarta Persistence environment. Trying to instantiate Jakarta Persistence aware TraversableResolver",
PERSISTENCE_CLASS_NAME,
PERSISTENCE_UTIL_METHOD
);
Expand All @@ -99,7 +99,7 @@ public static TraversableResolver getDefault() {
Class<? extends TraversableResolver> jpaAwareResolverClass = (Class<? extends TraversableResolver>)
LoadClass.action( JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME, TraversableResolvers.class.getClassLoader() );
LOG.debugf(
"Instantiated JPA aware TraversableResolver of type %s.", JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
"Instantiated Jakarta Persistence aware TraversableResolver of type %s.", JPA_AWARE_TRAVERSABLE_RESOLVER_CLASS_NAME
);
return NewInstance.action( jpaAwareResolverClass, "" );
}
Expand All @@ -117,7 +117,7 @@ public static TraversableResolver getDefault() {
* <p>
* If {@code traversableResolver} is an instance of our {@code JPATraversableResolver}, we wrap it with a caching
* wrapper specially tailored for the requirements of the spec. It is a very common case as it is used as soon as we
* have a JPA implementation in the classpath so optimizing this case is worth it.
* have a Jakarta Persistence implementation in the classpath so optimizing this case is worth it.
* <p>
* In all the other cases, we wrap the resolver for caching.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* {@link TraversableResolver} considering that all properties are reachable and cascadable.
* <p>
* This is the default behavior if JPA is not detected in the classpath.
* This is the default behavior if Jakarta Persistence is not detected in the classpath.
*
* @author Guillaume Smet
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ abstract class AbstractValidationContext<T> implements BaseBeanValidationContext
protected final ValidatorScopedContext validatorScopedContext;

/**
* Allows a JPA provider to decide whether a property should be validated.
* Allows a Jakarta Persistence provider to decide whether a property should be validated.
*/
private final TraversableResolver traversableResolver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ ValidationException getUnableToAccessMethodException(Lookup lookup, @FormatWith(
ValidationException logUnknownElementInXmlConfiguration(String tag);

@LogMessage(level = WARN)
@Message(id = 242, value = "Unable to load or instantiate JPA aware resolver %1$s. All properties will per default be traversable.")
@Message(id = 242, value = "Unable to load or instantiate Jakarta Persistence aware resolver %1$s. All properties will per default be traversable.")
void logUnableToLoadOrInstantiateJPAAwareResolver(String traversableResolverClassName);

@Message(id = 243, value = "Constraint %2$s references constraint validator type %1$s, but this validator is defined for constraint type %3$s.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import jakarta.validation.ConstraintViolationException;

/**
* Tests the usage of HV by JPA, applying a custom validation.xml. Also making sure that the VF is CDI-enabled.
* Tests the usage of HV by Jakarta Persistence, applying a custom validation.xml. Also making sure that the VF is CDI-enabled.
*
* @author Hardy Ferentschik
* @author Gunnar Morling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import jakarta.validation.TraversableResolver;

/**
* Tests that the default {@link TraversableResolver} for a JPA environment is {@code JPATraversableResolver}.
* Tests that the default {@link TraversableResolver} for a Jakarta Persistence environment is {@code JPATraversableResolver}.
*
* @author Guillaume Smet
*/
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
<!-- Maven version required for the build -->
<!-- Remember to update README when changing the version here. -->
<maven.min.version>3.9.6</maven.min.version>
<version.jakarta.persistence>3.2</version.jakarta.persistence>
</properties>

<dependencyManagement>
Expand Down

0 comments on commit a6a780f

Please sign in to comment.