Skip to content

Commit 5501336

Browse files
committed
1 parent cc1ce68 commit 5501336

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/beanvalidation/ddl/NotNullSchemaConstraint.java

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package org.hibernate.cfg.beanvalidation.ddl;
2525

2626
import java.util.Iterator;
27+
2728
import javax.validation.constraints.NotNull;
2829
import javax.validation.metadata.ConstraintDescriptor;
2930
import javax.validation.metadata.PropertyDescriptor;
@@ -33,6 +34,7 @@
3334
import org.hibernate.mapping.Property;
3435
import org.hibernate.mapping.SingleTableSubclass;
3536
import org.hibernate.metamodel.spi.binding.AttributeBinding;
37+
import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding;
3638
import org.hibernate.metamodel.spi.binding.EntityBinding;
3739
import org.hibernate.metamodel.spi.binding.InheritanceType;
3840

@@ -74,15 +76,34 @@ public boolean applyConstraint(AttributeBinding attributeBinding, ConstraintDesc
7476
if(InheritanceType.SINGLE_TABLE.equals( inheritanceType )) {
7577
return false;
7678
}
77-
78-
org.hibernate.metamodel.spi.relational.Column column = SchemaModificationHelper.getSingleColumn( attributeBinding );
79-
if ( column == null ) {
80-
return false;
79+
80+
// If a Composite (Embeddable static type, etc.), use the attribute's
81+
// nullability on the static type's fields.
82+
83+
// TODO: Is this the correct place to do this? Higher up in
84+
// TypeSafeActivator?
85+
if ( attributeBinding instanceof CompositeAttributeBinding ) {
86+
Iterator<AttributeBinding> iter
87+
= ( ( CompositeAttributeBinding ) attributeBinding)
88+
.attributeBindings().iterator();
89+
while( iter.hasNext() ) {
90+
applyNullConstraint( iter.next() );
91+
}
92+
} else {
93+
applyNullConstraint( attributeBinding );
8194
}
8295

83-
// TODO check with components as in the old configuration approach. see above (HF)
84-
column.setNullable( false );
96+
8597

8698
return true;
8799
}
100+
101+
private void applyNullConstraint(AttributeBinding attributeBinding) {
102+
org.hibernate.metamodel.spi.relational.Column column
103+
= SchemaModificationHelper.getSingleColumn( attributeBinding );
104+
if ( column != null ) {
105+
// TODO check with components as in the old configuration approach. see above (HF)
106+
column.setNullable( false );
107+
}
108+
}
88109
}

hibernate-core/src/test/java/org/hibernate/test/annotations/beanvalidation/DDLTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,16 @@ public void testSingleTableAvoidNotNull() throws Exception {
7979
assertTrue( "Notnull should not be applied on single tables", column.isNullable() );
8080
}
8181

82-
@FailureExpectedWithNewMetamodel
8382
@Test
8483
public void testNotNullOnlyAppliedIfEmbeddedIsNotNullItself() throws Exception {
85-
Column column = SchemaUtil.getColumn( Tv.class, "tuner.frequency", metadata() );
84+
Column column = SchemaUtil.getColumn( Tv.class, "frequency", metadata() );
8685
assertEquals(
8786
"Validator annotations are applied on tuner as it is @NotNull", false, column.isNullable()
8887
);
8988

90-
column = SchemaUtil.getColumn( Tv.class, "recorder.time", metadata() );
89+
column = SchemaUtil.getColumn( Tv.class, "time", metadata() );
9190
assertEquals(
92-
"Validator annotations are applied on tuner as it is @NotNull", true, column.isNullable()
91+
"Validator annotations were not applied on recorder", true, column.isNullable()
9392
);
9493
}
9594

0 commit comments

Comments
 (0)