Add Hibernate 7 batches 2-6, batch 7, and batch 8: mapping styles, JPA annotations, natural IDs, @Immutable, stored procedures, in-memory test databases, JNDI mocking, proxies, associations, temporal mapping, named queries, HQL, Criteria API, EntityManager bootstrapping, Ehcache 3 L2 cache configuration, HikariCP connection pooling, Hibernate Validator CDI integration, aggregate functions, sorting, pagination, interceptors, and Hibernate Search 8 (Hibernate 7.4.5.Final + Spring Boot 4.1.1 + JDK 25)
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.ankurm.hibernatedemo.validation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validator;
|
||||
import java.util.Set;
|
||||
import org.jboss.weld.environment.se.Weld;
|
||||
import org.jboss.weld.environment.se.WeldContainer;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The other half of the measurement {@link PlainValidationNoCdiTest} started: run the identical
|
||||
* {@link StockLevel}/{@link PositiveInventoryValidator} pair inside a real, standalone CDI
|
||||
* container (Weld SE -- no Jakarta EE server) with {@code hibernate-validator-cdi} on the
|
||||
* classpath, and see whether {@code @Inject} actually works this time.
|
||||
*
|
||||
* <p>It does, and the mechanism is worth naming rather than treating as magic: {@code
|
||||
* hibernate-validator-cdi-9.1.3.Final.jar} registers {@code
|
||||
* org.hibernate.validator.cdi.ValidationExtension} as a {@code jakarta.enterprise.inject.spi.Extension}
|
||||
* (confirmed via its {@code META-INF/services/jakarta.enterprise.inject.spi.Extension} file).
|
||||
* Once Weld picks that extension up, it contributes CDI beans for {@code Validator}/{@code
|
||||
* ValidatorFactory} whose {@code ConstraintValidatorFactory} is {@code
|
||||
* org.hibernate.validator.cdi.spi.InjectingConstraintValidatorFactory} -- a factory that builds
|
||||
* each {@code ConstraintValidator} instance through the CDI {@code BeanManager} instead of plain
|
||||
* reflection, so {@code @Inject} fields on it are resolved like any other managed bean's.
|
||||
*
|
||||
* <p>Docs: docs/20-hibernate-validator-cdi.md
|
||||
*/
|
||||
class CdiValidationTest {
|
||||
|
||||
private WeldContainer container;
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
if (container != null) {
|
||||
container.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void constraintValidatorsInjectField_isProperlyPopulated_insideARunningCdiContainer() {
|
||||
container = new Weld().initialize();
|
||||
Validator validator = container.select(Validator.class).get();
|
||||
|
||||
Set<ConstraintViolation<StockLevel>> belowThreshold = validator.validate(new StockLevel(3));
|
||||
Set<ConstraintViolation<StockLevel>> atThreshold = validator.validate(new StockLevel(5));
|
||||
Set<ConstraintViolation<StockLevel>> aboveThreshold = validator.validate(new StockLevel(10));
|
||||
|
||||
System.out.println("RESULT[cdi-validation-injection-works]: validator obtained from a running "
|
||||
+ "Weld SE container | StockLevel(3) violations=" + belowThreshold.size()
|
||||
+ " | StockLevel(5) violations=" + atThreshold.size()
|
||||
+ " | StockLevel(10) violations=" + aboveThreshold.size()
|
||||
+ " -- InventoryPolicy.minimumThreshold()=5 was actually injected and actually used, "
|
||||
+ "no NullPointerException anywhere.");
|
||||
|
||||
assertThat(belowThreshold)
|
||||
.as("3 is below the injected policy's threshold of 5 -- a real constraint violation, "
|
||||
+ "not an exception")
|
||||
.hasSize(1);
|
||||
assertThat(atThreshold)
|
||||
.as("5 meets the threshold exactly")
|
||||
.isEmpty();
|
||||
assertThat(aboveThreshold)
|
||||
.as("10 comfortably clears the threshold")
|
||||
.isEmpty();
|
||||
|
||||
assertThat(belowThreshold.iterator().next().getMessage())
|
||||
.isEqualTo("quantity is below the minimum inventory threshold");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.ankurm.hibernatedemo.validation;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The baseline this chapter needs before the CDI test means anything: plain Jakarta Bean
|
||||
* Validation, bootstrapped the ordinary way with no CDI container running at all. {@code
|
||||
* Validation.buildDefaultValidatorFactory()} builds its {@code ConstraintValidatorFactory} by
|
||||
* plain reflection ({@code Class.newInstance()}-equivalent) -- it does not scan for {@code
|
||||
* @Inject} at all, so {@link PositiveInventoryValidator#policy} is left {@code null}.
|
||||
*
|
||||
* <p>Docs: docs/20-hibernate-validator-cdi.md
|
||||
*/
|
||||
class PlainValidationNoCdiTest {
|
||||
|
||||
@Test
|
||||
void constraintValidatorsInjectField_isNeverPopulated_withoutACdiContainer() {
|
||||
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
|
||||
Validator validator = factory.getValidator();
|
||||
|
||||
Throwable thrown = catchThrowable(() -> validator.validate(new StockLevel(3)));
|
||||
|
||||
System.out.println("RESULT[cdi-plain-validation-no-injection]: validating StockLevel(3) with "
|
||||
+ "Validation.buildDefaultValidatorFactory() (no CDI container running) throws "
|
||||
+ describeChain(thrown));
|
||||
|
||||
assertThat(thrown).isNotNull();
|
||||
Throwable rootCause = rootCause(thrown);
|
||||
assertThat(rootCause)
|
||||
.as("the @Inject-annotated policy field was never populated, so calling "
|
||||
+ "policy.minimumThreshold() inside isValid() throws a plain NullPointerException "
|
||||
+ "-- not a validation failure, a validator bug caused by the missing CDI container")
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
|
||||
factory.close();
|
||||
}
|
||||
|
||||
private static Throwable catchThrowable(ThrowingRunnable runnable) {
|
||||
try {
|
||||
runnable.run();
|
||||
return null;
|
||||
} catch (Throwable t) {
|
||||
return t;
|
||||
}
|
||||
}
|
||||
|
||||
private static Throwable rootCause(Throwable t) {
|
||||
Throwable cause = t;
|
||||
while (cause.getCause() != null && cause.getCause() != cause) {
|
||||
cause = cause.getCause();
|
||||
}
|
||||
return cause;
|
||||
}
|
||||
|
||||
private static String describeChain(Throwable t) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Throwable current = t;
|
||||
while (current != null) {
|
||||
sb.append(current.getClass().getName());
|
||||
if (current.getCause() != null && current.getCause() != current) {
|
||||
sb.append(" -> caused by ");
|
||||
}
|
||||
current = current.getCause() == current ? null : current.getCause();
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ThrowingRunnable {
|
||||
void run() throws Exception;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user