Skip to content

Commit 5c0a4cb

Browse files
committed
HHH-19614 Validation @NotNull should mark whole property optional=false
1 parent a74cc2a commit 5c0a4cb

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private static boolean applyConstraints(
311311

312312
// Apply Hibernate Validator specific constraints - we cannot import any HV specific classes though!
313313
// No need to check explicitly for @Range. @Range is a composed constraint using @Min and @Max which
314-
// will be taken care later.
314+
// will be taken care of later.
315315
applyLength( property, descriptor, propertyDesc );
316316

317317
// Composing constraints
@@ -360,7 +360,7 @@ private static boolean isConstraintCompositionOfTypeOr(
360360
return false;
361361
}
362362

363-
final Class<? extends Annotation> composedAnnotation = descriptor.getAnnotation().annotationType();
363+
final var composedAnnotation = descriptor.getAnnotation().annotationType();
364364
return constraintCompositionTypeCache.computeIfAbsent( composedAnnotation, value -> {
365365
for ( Annotation annotation : value.getAnnotations() ) {
366366
if ( "org.hibernate.validator.constraints.ConstraintComposition"
@@ -383,7 +383,7 @@ private static boolean isConstraintCompositionOfTypeOr(
383383
private static void applyMin(Property property, ConstraintDescriptor<?> descriptor, Dialect dialect) {
384384
if ( Min.class.equals( descriptor.getAnnotation().annotationType() ) ) {
385385
@SuppressWarnings("unchecked")
386-
final ConstraintDescriptor<Min> minConstraint = (ConstraintDescriptor<Min>) descriptor;
386+
final var minConstraint = (ConstraintDescriptor<Min>) descriptor;
387387
final long min = minConstraint.getAnnotation().value();
388388
for ( Selectable selectable : property.getSelectables() ) {
389389
if ( selectable instanceof Column column ) {
@@ -396,7 +396,7 @@ private static void applyMin(Property property, ConstraintDescriptor<?> descript
396396
private static void applyMax(Property property, ConstraintDescriptor<?> descriptor, Dialect dialect) {
397397
if ( Max.class.equals( descriptor.getAnnotation().annotationType() ) ) {
398398
@SuppressWarnings("unchecked")
399-
final ConstraintDescriptor<Max> maxConstraint = (ConstraintDescriptor<Max>) descriptor;
399+
final var maxConstraint = (ConstraintDescriptor<Max>) descriptor;
400400
final long max = maxConstraint.getAnnotation().value();
401401
for ( Selectable selectable : property.getSelectables() ) {
402402
if ( selectable instanceof Column column ) {
@@ -420,15 +420,16 @@ private static void applySQLCheck(Column column, String checkConstraint) {
420420
private static boolean isNotNullDescriptor(ConstraintDescriptor<?> descriptor) {
421421
final Class<? extends Annotation> annotationType = descriptor.getAnnotation().annotationType();
422422
return NotNull.class.equals(annotationType)
423-
|| NotEmpty.class.equals(annotationType)
424-
|| NotBlank.class.equals(annotationType);
423+
|| NotEmpty.class.equals(annotationType)
424+
|| NotBlank.class.equals(annotationType);
425425
}
426426

427427
private static void markNotNull(Property property) {
428428
// single table inheritance should not be forced to null due to shared state
429429
if ( !( property.getPersistentClass() instanceof SingleTableSubclass ) ) {
430430
// composite should not add not-null on all columns
431431
if ( !property.isComposite() ) {
432+
property.setOptional( false );
432433
for ( Selectable selectable : property.getSelectables() ) {
433434
if ( selectable instanceof Column column ) {
434435
column.setNullable( false );
@@ -449,7 +450,7 @@ private static void markNotNull(Property property) {
449450
private static void applyDigits(Property property, ConstraintDescriptor<?> descriptor) {
450451
if ( Digits.class.equals( descriptor.getAnnotation().annotationType() ) ) {
451452
@SuppressWarnings("unchecked")
452-
final ConstraintDescriptor<Digits> digitsConstraint = (ConstraintDescriptor<Digits>) descriptor;
453+
final var digitsConstraint = (ConstraintDescriptor<Digits>) descriptor;
453454
final int integerDigits = digitsConstraint.getAnnotation().integer();
454455
final int fractionalDigits = digitsConstraint.getAnnotation().fraction();
455456
for ( Selectable selectable : property.getSelectables() ) {
@@ -466,7 +467,7 @@ private static void applySize(Property property, ConstraintDescriptor<?> descrip
466467
if ( Size.class.equals( descriptor.getAnnotation().annotationType() )
467468
&& String.class.equals( propertyDescriptor.getElementClass() ) ) {
468469
@SuppressWarnings("unchecked")
469-
final ConstraintDescriptor<Size> sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
470+
final var sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
470471
final int max = sizeConstraint.getAnnotation().max();
471472
for ( Column col : property.getColumns() ) {
472473
if ( max < Integer.MAX_VALUE ) {
@@ -520,19 +521,19 @@ private static Property findPropertyByName(PersistentClass associatedClass, Stri
520521
property = associatedClass.getProperty( element );
521522
}
522523
else {
523-
if ( !property.isComposite() ) {
524-
return null;
524+
if ( property.isComposite() ) {
525+
property = ( (Component) property.getValue() ).getProperty( element );
525526
}
526527
else {
527-
property = ( (Component) property.getValue() ).getProperty( element );
528+
return null;
528529
}
529530
}
530531
}
531532
}
532533
}
533534
catch ( MappingException e ) {
534535
try {
535-
//if we do not find it try to check the identifier mapper
536+
//if we do not find it, try to check the identifier mapper
536537
if ( associatedClass.getIdentifierMapper() == null ) {
537538
return null;
538539
}
@@ -544,11 +545,11 @@ private static Property findPropertyByName(PersistentClass associatedClass, Stri
544545
property = associatedClass.getIdentifierMapper().getProperty( element );
545546
}
546547
else {
547-
if ( !property.isComposite() ) {
548-
return null;
548+
if ( property.isComposite() ) {
549+
property = ( (Component) property.getValue() ).getProperty( element );
549550
}
550551
else {
551-
property = ( (Component) property.getValue() ).getProperty( element );
552+
return null;
552553
}
553554
}
554555
}
@@ -562,8 +563,9 @@ private static Property findPropertyByName(PersistentClass associatedClass, Stri
562563
}
563564

564565
private static ValidatorFactory getValidatorFactory(ActivationContext context) {
565-
// IMPL NOTE : We can either be provided a ValidatorFactory or make one. We can be provided
566-
// a ValidatorFactory in 2 different ways. So here we "get" a ValidatorFactory in the following order:
566+
// IMPL NOTE: We can either be provided a ValidatorFactory or make one. We can be provided
567+
// a ValidatorFactory in 2 different ways. So here we "get" a ValidatorFactory
568+
// in the following order:
567569
// 1) Look into SessionFactoryOptions.getValidatorFactoryReference()
568570
// 2) Look into ConfigurationService
569571
// 3) build a new ValidatorFactory

0 commit comments

Comments
 (0)