Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,24 @@ public class BeanValidationEventListener

private SessionFactoryImplementor sessionFactory;

public BeanValidationEventListener(ValidatorFactory factory, Map<String, Object> settings, ClassLoaderService classLoaderService) {
public BeanValidationEventListener(
ValidatorFactory factory, Map<String, Object> settings, ClassLoaderService classLoaderService) {
traversableResolver = new HibernateTraversableResolver();
validator =
factory.usingContext()
.traversableResolver( traversableResolver )
.getValidator();
groupsPerOperation = GroupsPerOperation.from( settings, new ClassLoaderAccessImpl( classLoaderService ) );
groupsPerOperation =
GroupsPerOperation.from( settings,
new ClassLoaderAccessImpl( classLoaderService ) );
}

@Override
public void sessionFactoryCreated(SessionFactory factory) {
sessionFactory = factory.unwrap( SessionFactoryImplementor.class );
sessionFactory.getMappingMetamodel()
.forEachEntityDescriptor( entityPersister -> traversableResolver.addPersister( entityPersister, sessionFactory ) );
.forEachEntityDescriptor( entityPersister ->
traversableResolver.addPersister( entityPersister, sessionFactory ) );
}

public boolean onPreInsert(PreInsertEvent event) {
Expand Down Expand Up @@ -117,58 +121,64 @@ public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
);
}

private EntityPersister getEntityPersister(SharedSessionContractImplementor session, String entityName, Object entity) {
private EntityPersister getEntityPersister(
SharedSessionContractImplementor session, String entityName, Object entity) {
if ( session != null ) {
return session.getEntityPersister( entityName, entity );
}
return entityName == null
? sessionFactory.getMappingMetamodel().getEntityDescriptor( entity.getClass().getName() )
: sessionFactory.getMappingMetamodel().getEntityDescriptor( entityName )
.getSubclassEntityPersister( entity, sessionFactory );
else {
final var metamodel = sessionFactory.getMappingMetamodel();
return entityName == null
? metamodel.getEntityDescriptor( entity.getClass().getName() )
: metamodel.getEntityDescriptor( entityName )
.getSubclassEntityPersister( entity, sessionFactory );
}
}

private <T> void validate(
T object,
EntityPersister persister,
GroupsPerOperation.Operation operation) {
private <T> void validate(T object, EntityPersister persister, GroupsPerOperation.Operation operation) {
if ( object != null
&& persister.getRepresentationStrategy().getMode() == RepresentationMode.POJO ) {
final Class<?>[] groups = groupsPerOperation.get( operation );
final var groups = groupsPerOperation.get( operation );
if ( groups.length > 0 ) {
final var constraintViolations = validator.validate( object, groups );
if ( !constraintViolations.isEmpty() ) {
final Set<ConstraintViolation<?>> propagatedViolations = setOfSize( constraintViolations.size() );
final Set<ConstraintViolation<?>> propagatedViolations =
setOfSize( constraintViolations.size() );
final Set<String> classNames = new HashSet<>();
for ( var violation : constraintViolations ) {
BEAN_VALIDATION_LOGGER.trace( violation );
propagatedViolations.add( violation );
classNames.add( violation.getLeafBean().getClass().getName() );
}
final var builder =
new StringBuilder()
.append( "Validation failed for classes " )
.append( classNames )
.append( " during " )
.append( operation.getName() )
.append( " time for groups " )
.append( toString( groups ) )
.append( "\nList of constraint violations:[\n" );
for ( var violation : constraintViolations ) {
builder.append( "\t" ).append( violation.toString() ).append( "\n" );
}
builder.append( "]" );
throw new ConstraintViolationException( builder.toString(), propagatedViolations );
throw new ConstraintViolationException(
message( operation, classNames, groups, constraintViolations ),
propagatedViolations );
}
}
}
}

private String toString(Class<?>[] groups) {
final var string = new StringBuilder( "[" );
private <T> String message(
GroupsPerOperation.Operation operation,
Set<String> classNames,
Class<?>[] groups,
Set<ConstraintViolation<T>> constraintViolations) {
final var builder = new StringBuilder();
builder.append( "Validation failed for classes " )
.append( classNames )
.append( " during " )
.append( operation.getName() )
.append( " time for groups [" );
for ( var group : groups ) {
string.append( group.getName() ).append( ", " );
builder.append( group.getName() ).append( ", " );
}
builder.append( "]\nList of constraint violations:[\n" );
for ( var violation : constraintViolations ) {
builder.append( "\t" )
.append( violation.toString() )
.append( "\n" );
}
string.append( "]" );
return string.toString();
builder.append( "]" );
return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ else if ( validationModes.contains( ValidationMode.DDL ) ) {
// There is a Jakarta Validation provider, but it failed to bootstrap the factory for some reason,
// we should fail and let the user deal with it:
throw exception;

}
}
}
Expand Down Expand Up @@ -148,10 +147,10 @@ private static void setupListener(
ValidatorFactory validatorFactory,
SessionFactoryServiceRegistry serviceRegistry,
SessionFactoryImplementor sessionFactory) {
final var classLoaderService = serviceRegistry.requireService( ClassLoaderService.class );
final var cfgService = serviceRegistry.requireService( ConfigurationService.class );
final var listener =
new BeanValidationEventListener( validatorFactory, cfgService.getSettings(), classLoaderService );
new BeanValidationEventListener( validatorFactory,
serviceRegistry.requireService( ConfigurationService.class ).getSettings(),
serviceRegistry.requireService( ClassLoaderService.class ) );
final var listenerRegistry = sessionFactory.getEventListenerRegistry();
listenerRegistry.addDuplicationStrategy( DuplicationStrategyImpl.INSTANCE );
listenerRegistry.appendListeners( EventType.PRE_INSERT, listener );
Expand Down Expand Up @@ -183,7 +182,8 @@ private static void applyRelationalConstraints(ValidatorFactory factory, Activat
context.getMetadata().getEntityBindings(),
serviceRegistry.requireService( ConfigurationService.class ).getSettings(),
serviceRegistry.requireService( JdbcServices.class ).getDialect(),
new ClassLoaderAccessImpl( null, serviceRegistry.getService( ClassLoaderService.class ) )
new ClassLoaderAccessImpl( null,
serviceRegistry.getService( ClassLoaderService.class ) )
);
}
}
Expand Down