Skip to content

Commit fee7ed7

Browse files
committed
minor cleanups to BeanValidationEventListener
1 parent eb898b1 commit fee7ed7

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/BeanValidationEventListener.java

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,24 @@ public class BeanValidationEventListener
5454

5555
private SessionFactoryImplementor sessionFactory;
5656

57-
public BeanValidationEventListener(ValidatorFactory factory, Map<String, Object> settings, ClassLoaderService classLoaderService) {
57+
public BeanValidationEventListener(
58+
ValidatorFactory factory, Map<String, Object> settings, ClassLoaderService classLoaderService) {
5859
traversableResolver = new HibernateTraversableResolver();
5960
validator =
6061
factory.usingContext()
6162
.traversableResolver( traversableResolver )
6263
.getValidator();
63-
groupsPerOperation = GroupsPerOperation.from( settings, new ClassLoaderAccessImpl( classLoaderService ) );
64+
groupsPerOperation =
65+
GroupsPerOperation.from( settings,
66+
new ClassLoaderAccessImpl( classLoaderService ) );
6467
}
6568

6669
@Override
6770
public void sessionFactoryCreated(SessionFactory factory) {
6871
sessionFactory = factory.unwrap( SessionFactoryImplementor.class );
6972
sessionFactory.getMappingMetamodel()
70-
.forEachEntityDescriptor( entityPersister -> traversableResolver.addPersister( entityPersister, sessionFactory ) );
73+
.forEachEntityDescriptor( entityPersister ->
74+
traversableResolver.addPersister( entityPersister, sessionFactory ) );
7175
}
7276

7377
public boolean onPreInsert(PreInsertEvent event) {
@@ -117,58 +121,64 @@ public void onPreUpdateCollection(PreCollectionUpdateEvent event) {
117121
);
118122
}
119123

120-
private EntityPersister getEntityPersister(SharedSessionContractImplementor session, String entityName, Object entity) {
124+
private EntityPersister getEntityPersister(
125+
SharedSessionContractImplementor session, String entityName, Object entity) {
121126
if ( session != null ) {
122127
return session.getEntityPersister( entityName, entity );
123128
}
124-
return entityName == null
125-
? sessionFactory.getMappingMetamodel().getEntityDescriptor( entity.getClass().getName() )
126-
: sessionFactory.getMappingMetamodel().getEntityDescriptor( entityName )
127-
.getSubclassEntityPersister( entity, sessionFactory );
129+
else {
130+
final var metamodel = sessionFactory.getMappingMetamodel();
131+
return entityName == null
132+
? metamodel.getEntityDescriptor( entity.getClass().getName() )
133+
: metamodel.getEntityDescriptor( entityName )
134+
.getSubclassEntityPersister( entity, sessionFactory );
135+
}
128136
}
129137

130-
private <T> void validate(
131-
T object,
132-
EntityPersister persister,
133-
GroupsPerOperation.Operation operation) {
138+
private <T> void validate(T object, EntityPersister persister, GroupsPerOperation.Operation operation) {
134139
if ( object != null
135140
&& persister.getRepresentationStrategy().getMode() == RepresentationMode.POJO ) {
136-
final Class<?>[] groups = groupsPerOperation.get( operation );
141+
final var groups = groupsPerOperation.get( operation );
137142
if ( groups.length > 0 ) {
138143
final var constraintViolations = validator.validate( object, groups );
139144
if ( !constraintViolations.isEmpty() ) {
140-
final Set<ConstraintViolation<?>> propagatedViolations = setOfSize( constraintViolations.size() );
145+
final Set<ConstraintViolation<?>> propagatedViolations =
146+
setOfSize( constraintViolations.size() );
141147
final Set<String> classNames = new HashSet<>();
142148
for ( var violation : constraintViolations ) {
143149
BEAN_VALIDATION_LOGGER.trace( violation );
144150
propagatedViolations.add( violation );
145151
classNames.add( violation.getLeafBean().getClass().getName() );
146152
}
147-
final var builder =
148-
new StringBuilder()
149-
.append( "Validation failed for classes " )
150-
.append( classNames )
151-
.append( " during " )
152-
.append( operation.getName() )
153-
.append( " time for groups " )
154-
.append( toString( groups ) )
155-
.append( "\nList of constraint violations:[\n" );
156-
for ( var violation : constraintViolations ) {
157-
builder.append( "\t" ).append( violation.toString() ).append( "\n" );
158-
}
159-
builder.append( "]" );
160-
throw new ConstraintViolationException( builder.toString(), propagatedViolations );
153+
throw new ConstraintViolationException(
154+
message( operation, classNames, groups, constraintViolations ),
155+
propagatedViolations );
161156
}
162157
}
163158
}
164159
}
165160

166-
private String toString(Class<?>[] groups) {
167-
final var string = new StringBuilder( "[" );
161+
private <T> String message(
162+
GroupsPerOperation.Operation operation,
163+
Set<String> classNames,
164+
Class<?>[] groups,
165+
Set<ConstraintViolation<T>> constraintViolations) {
166+
final var builder = new StringBuilder();
167+
builder.append( "Validation failed for classes " )
168+
.append( classNames )
169+
.append( " during " )
170+
.append( operation.getName() )
171+
.append( " time for groups [" );
168172
for ( var group : groups ) {
169-
string.append( group.getName() ).append( ", " );
173+
builder.append( group.getName() ).append( ", " );
174+
}
175+
builder.append( "]\nList of constraint violations:[\n" );
176+
for ( var violation : constraintViolations ) {
177+
builder.append( "\t" )
178+
.append( violation.toString() )
179+
.append( "\n" );
170180
}
171-
string.append( "]" );
172-
return string.toString();
181+
builder.append( "]" );
182+
return builder.toString();
173183
}
174184
}

hibernate-core/src/main/java/org/hibernate/boot/beanvalidation/TypeSafeActivator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ else if ( validationModes.contains( ValidationMode.DDL ) ) {
106106
// There is a Jakarta Validation provider, but it failed to bootstrap the factory for some reason,
107107
// we should fail and let the user deal with it:
108108
throw exception;
109-
110109
}
111110
}
112111
}
@@ -148,10 +147,10 @@ private static void setupListener(
148147
ValidatorFactory validatorFactory,
149148
SessionFactoryServiceRegistry serviceRegistry,
150149
SessionFactoryImplementor sessionFactory) {
151-
final var classLoaderService = serviceRegistry.requireService( ClassLoaderService.class );
152-
final var cfgService = serviceRegistry.requireService( ConfigurationService.class );
153150
final var listener =
154-
new BeanValidationEventListener( validatorFactory, cfgService.getSettings(), classLoaderService );
151+
new BeanValidationEventListener( validatorFactory,
152+
serviceRegistry.requireService( ConfigurationService.class ).getSettings(),
153+
serviceRegistry.requireService( ClassLoaderService.class ) );
155154
final var listenerRegistry = sessionFactory.getEventListenerRegistry();
156155
listenerRegistry.addDuplicationStrategy( DuplicationStrategyImpl.INSTANCE );
157156
listenerRegistry.appendListeners( EventType.PRE_INSERT, listener );
@@ -183,7 +182,8 @@ private static void applyRelationalConstraints(ValidatorFactory factory, Activat
183182
context.getMetadata().getEntityBindings(),
184183
serviceRegistry.requireService( ConfigurationService.class ).getSettings(),
185184
serviceRegistry.requireService( JdbcServices.class ).getDialect(),
186-
new ClassLoaderAccessImpl( null, serviceRegistry.getService( ClassLoaderService.class ) )
185+
new ClassLoaderAccessImpl( null,
186+
serviceRegistry.getService( ClassLoaderService.class ) )
187187
);
188188
}
189189
}

0 commit comments

Comments
 (0)