Skip to content

Commit c01d67a

Browse files
committed
HHH-19614 fix bug where @manytoone(optional=false) FKs for single table inheritance were 'not null'
1 parent a2c2631 commit c01d67a

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1757,7 +1757,7 @@ public void processSecondPasses(MetadataBuildingContext buildingContext) {
17571757

17581758
processFkSecondPassesInOrder();
17591759

1760-
processSecondPasses(createKeySecondPassList);
1760+
processSecondPasses( createKeySecondPassList );
17611761
processSecondPasses( secondaryTableSecondPassList );
17621762

17631763
processSecondPasses( querySecondPassList );

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ColumnsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ else if ( columnsAnn != null ) {
129129
else if ( joinColumns == null
130130
&& ( property.hasDirectAnnotationUsage( OneToMany.class )
131131
|| property.hasDirectAnnotationUsage( ElementCollection.class ) ) ) {
132-
OneToMany oneToMany = property.getDirectAnnotationUsage( OneToMany.class );
132+
final OneToMany oneToMany = property.getDirectAnnotationUsage( OneToMany.class );
133133
joinColumns = AnnotatedJoinColumns.buildJoinColumns(
134134
null,
135135
oneToMany == null ? null : nullIfEmpty( oneToMany.mappedBy() ),

hibernate-core/src/main/java/org/hibernate/boot/model/internal/EntityBinder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,11 @@ public static void bindEntityClass(
220220
superEntity.addSubclass( subclass );
221221
}
222222

223-
persistentClass.createConstraints( context );
224-
225223
collector.addEntityBinding( persistentClass );
226224
// process secondary tables and complementary definitions (ie o.h.a.Table)
227225
collector.addSecondPass( new SecondaryTableFromAnnotationSecondPass( entityBinder, holder ) );
228226
collector.addSecondPass( new SecondaryTableSecondPass( entityBinder, holder ) );
227+
collector.addSecondPass( ignored -> persistentClass.createConstraints( context ) );
229228
// comment, checkConstraint, and indexes are processed here
230229
entityBinder.processComplementaryTableDefinitions();
231230
resolveLifecycleCallbacks( clazzToProcess, persistentClass, context.getMetadataCollector() );

hibernate-core/src/main/java/org/hibernate/boot/model/internal/PropertyBinder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ private AnnotatedColumns bindProperty(
859859
else if ( isManyToOne( property ) ) {
860860
bindManyToOne(
861861
propertyHolder,
862+
nullability,
862863
inferredData,
863864
isIdentifierMapper,
864865
inSecondPass,
@@ -870,6 +871,7 @@ else if ( isManyToOne( property ) ) {
870871
else if ( isOneToOne( property ) ) {
871872
bindOneToOne(
872873
propertyHolder,
874+
nullability,
873875
inferredData,
874876
isIdentifierMapper,
875877
inSecondPass,

hibernate-core/src/main/java/org/hibernate/boot/model/internal/ToOneBinder.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class ToOneBinder {
7171

7272
static void bindManyToOne(
7373
PropertyHolder propertyHolder,
74+
Nullability nullability,
7475
PropertyData inferredData,
7576
boolean isIdentifierMapper,
7677
boolean inSecondPass,
@@ -102,6 +103,7 @@ && isIdentifier( propertyHolder, propertyBinder, isIdentifierMapper ) ) {
102103
aggregateCascadeTypes( manyToOne.cascade(), hibernateCascade, false, context ),
103104
joinColumns,
104105
propertyHolder,
106+
nullability,
105107
inferredData,
106108
manyToOne.fetch(),
107109
manyToOne.optional(),
@@ -140,6 +142,7 @@ private static void bindManyToOne(
140142
EnumSet<CascadeType> cascadeStrategy,
141143
AnnotatedJoinColumns joinColumns,
142144
PropertyHolder propertyHolder,
145+
Nullability nullability,
143146
PropertyData inferredData,
144147
FetchType fetchType,
145148
boolean explicitlyOptional,
@@ -172,7 +175,7 @@ private static void bindManyToOne(
172175
manyToOne.setNotFoundAction( notFoundAction );
173176
manyToOne.setOnDeleteAction( onDeleteAction );
174177
//value.setLazy( fetchMode != FetchMode.JOIN );
175-
if ( !optional ) {
178+
if ( !optional && nullability != Nullability.FORCED_NULL ) {
176179
for ( AnnotatedJoinColumn column : joinColumns.getJoinColumns() ) {
177180
column.setNullable( false );
178181
}
@@ -413,6 +416,7 @@ else if ( oneToOne != null ) {
413416

414417
static void bindOneToOne(
415418
PropertyHolder propertyHolder,
419+
Nullability nullability,
416420
PropertyData inferredData,
417421
boolean isIdentifierMapper,
418422
boolean inSecondPass,
@@ -449,6 +453,7 @@ static void bindOneToOne(
449453
oneToOne.optional(),
450454
oneToOne.fetch(),
451455
propertyHolder,
456+
nullability,
452457
inferredData,
453458
nullIfEmpty( oneToOne.mappedBy() ),
454459
trueOneToOne,
@@ -465,6 +470,7 @@ private static void bindOneToOne(
465470
boolean explicitlyOptional,
466471
FetchType fetchMode,
467472
PropertyHolder propertyHolder,
473+
Nullability nullability,
468474
PropertyData inferredData,
469475
String mappedBy,
470476
boolean trueOneToOne,
@@ -492,6 +498,7 @@ private static void bindOneToOne(
492498
cascadeStrategy,
493499
joinColumns,
494500
propertyHolder,
501+
nullability,
495502
inferredData,
496503
fetchMode,
497504
explicitlyOptional,

0 commit comments

Comments
 (0)