@@ -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 ( "\n List 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 ( "]\n List 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}
0 commit comments