Skip to content

Commit a5f96b5

Browse files
committed
HHH-19542 Ensure columns in embeddables default to correct table
1 parent 49254cf commit a5f96b5

File tree

7 files changed

+28
-43
lines changed

7 files changed

+28
-43
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public boolean isSecondary() {
111111
final String explicitTableName = firstColumn.getExplicitTableName();
112112
//note: checkPropertyConsistency() is responsible for ensuring they all have the same table name
113113
return isNotEmpty( explicitTableName )
114-
&& !getPropertyHolder().getTable().getName().equals( explicitTableName );
114+
&& !getOwnerTable().getName().equals( explicitTableName );
115115
}
116116

117117
/**
@@ -130,10 +130,18 @@ public Table getTable() {
130130
// all the columns have to be mapped to the same table
131131
// even though at the annotation level it looks like
132132
// they could each specify a different table
133-
return isSecondary() ? getJoin().getTable() : getPropertyHolder().getTable();
133+
return isSecondary() ? getJoin().getTable() : getOwnerTable();
134134
}
135135
}
136136

137+
private Table getOwnerTable() {
138+
PropertyHolder holder = getPropertyHolder();
139+
while ( holder instanceof ComponentPropertyHolder componentPropertyHolder ) {
140+
holder = componentPropertyHolder.parent;
141+
}
142+
return holder.getTable();
143+
}
144+
137145
public void setTable(Table table) {
138146
this.table = table;
139147
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Map;
1111
import java.util.function.Consumer;
1212

13+
import org.checkerframework.checker.nullness.qual.Nullable;
1314
import org.hibernate.AssertionFailure;
1415
import org.hibernate.MappingException;
1516
import org.hibernate.PropertyNotFoundException;
@@ -150,7 +151,7 @@ public String getEntityName() {
150151
}
151152

152153
@Override
153-
public void addProperty(Property prop, MemberDetails memberDetails, AnnotatedColumns columns, ClassDetails declaringClass) {
154+
public void addProperty(Property prop, MemberDetails memberDetails, @Nullable AnnotatedColumns columns, ClassDetails declaringClass) {
154155
//AnnotatedColumn.checkPropertyConsistency( ); //already called earlier
155156
if ( columns != null ) {
156157
if ( columns.isSecondary() ) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Locale;
99
import java.util.Map;
1010

11+
import org.checkerframework.checker.nullness.qual.Nullable;
1112
import org.hibernate.AssertionFailure;
1213
import org.hibernate.annotations.CollectionType;
1314
import org.hibernate.annotations.ManyToAny;
@@ -301,7 +302,7 @@ public String getEntityName() {
301302
}
302303

303304
@Override
304-
public void addProperty(Property prop, MemberDetails memberDetails, AnnotatedColumns columns, ClassDetails declaringClass) {
305+
public void addProperty(Property prop, MemberDetails memberDetails, @Nullable AnnotatedColumns columns, ClassDetails declaringClass) {
305306
//Ejb3Column.checkPropertyConsistency( ); //already called earlier
306307
throw new AssertionFailure( "addProperty to a join table of a collection: does it make sense?" );
307308
}

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

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
*/
55
package org.hibernate.boot.model.internal;
66

7-
import java.util.ArrayList;
87
import java.util.HashMap;
9-
import java.util.List;
108
import java.util.Map;
11-
import java.util.Objects;
129

10+
import org.checkerframework.checker.nullness.qual.Nullable;
1311
import org.hibernate.AnnotationException;
1412
import org.hibernate.boot.spi.MetadataBuildingContext;
1513
import org.hibernate.boot.spi.PropertyData;
16-
import org.hibernate.internal.util.StringHelper;
1714
import org.hibernate.mapping.AggregateColumn;
1815
import org.hibernate.mapping.Component;
1916
import org.hibernate.mapping.Join;
@@ -72,7 +69,6 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
7269

7370
private final String embeddedAttributeName;
7471
private final Map<String,AttributeConversionInfo> attributeConversionInfoMap;
75-
private final List<AnnotatedColumn> annotatedColumns;
7672

7773
public ComponentPropertyHolder(
7874
Component component,
@@ -99,12 +95,6 @@ public ComponentPropertyHolder(
9995
this.embeddedAttributeName = "";
10096
this.attributeConversionInfoMap = processAttributeConversions( inferredData.getClassOrElementType() );
10197
}
102-
103-
if ( parent instanceof ComponentPropertyHolder parentHolder ) {
104-
this.annotatedColumns = parentHolder.annotatedColumns;
105-
} else {
106-
this.annotatedColumns = new ArrayList<>();
107-
}
10898
}
10999

110100
/**
@@ -231,12 +221,17 @@ public String getEntityName() {
231221
}
232222

233223
@Override
234-
public void addProperty(Property property, MemberDetails attributeMemberDetails, AnnotatedColumns columns, ClassDetails declaringClass) {
224+
public void addProperty(Property property, MemberDetails attributeMemberDetails, @Nullable AnnotatedColumns columns, ClassDetails declaringClass) {
235225
//AnnotatedColumns.checkPropertyConsistency( ); //already called earlier
236226
// Check table matches between the component and the columns
237227
// if not, change the component table if no properties are set
238228
// if a property is set already the core cannot support that
239-
final Table table = property.getValue().getTable();
229+
assert columns == null || property.getValue().getTable() == columns.getTable();
230+
setTable( property.getValue().getTable() );
231+
addProperty( property, attributeMemberDetails, declaringClass );
232+
}
233+
234+
private void setTable(Table table) {
240235
if ( !table.equals( getTable() ) ) {
241236
if ( component.getPropertySpan() == 0 ) {
242237
component.setTable( table );
@@ -248,27 +243,8 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
248243
+ " (all properties of the embeddable class must map to the same table)"
249244
);
250245
}
251-
}
252-
if ( columns != null ) {
253-
annotatedColumns.addAll( columns.getColumns() );
254-
}
255-
addProperty( property, attributeMemberDetails, declaringClass );
256-
}
257-
258-
public void checkPropertyConsistency() {
259-
if ( annotatedColumns.size() > 1 ) {
260-
for ( int currentIndex = 1; currentIndex < annotatedColumns.size(); currentIndex++ ) {
261-
final AnnotatedColumn current = annotatedColumns.get( currentIndex );
262-
final AnnotatedColumn previous = annotatedColumns.get( currentIndex - 1 );
263-
if ( !Objects.equals(
264-
StringHelper.nullIfEmpty( current.getExplicitTableName() ),
265-
StringHelper.nullIfEmpty( previous.getExplicitTableName() ) ) ) {
266-
throw new AnnotationException(
267-
"Embeddable class '" + component.getComponentClassName()
268-
+ "' has properties mapped to two different tables"
269-
+ " (all properties of the embeddable class must map to the same table)"
270-
);
271-
}
246+
if ( parent instanceof ComponentPropertyHolder parentComponentHolder ) {
247+
parentComponentHolder.setTable( table );
272248
}
273249
}
274250
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ static Component fillEmbeddable(
455455
if ( LOG.isDebugEnabled() ) {
456456
LOG.debug( "Binding component with path: " + subpath );
457457
}
458-
final ComponentPropertyHolder subholder = buildPropertyHolder(
458+
final PropertyHolder subholder = buildPropertyHolder(
459459
component,
460460
subpath,
461461
inferredData,
@@ -579,8 +579,6 @@ else if ( member.hasDirectAnnotationUsage( GeneratedValue.class ) ) {
579579
}
580580
}
581581

582-
subholder.checkPropertyConsistency();
583-
584582
if ( compositeUserType != null ) {
585583
processCompositeUserType( component, compositeUserType );
586584
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package org.hibernate.boot.model.internal;
66

7+
import org.checkerframework.checker.nullness.qual.Nullable;
78
import org.hibernate.annotations.ColumnTransformer;
89
import org.hibernate.boot.model.convert.spi.ConverterDescriptor;
910
import org.hibernate.mapping.Join;
@@ -33,7 +34,7 @@ public interface PropertyHolder {
3334

3435
void addProperty(Property prop, MemberDetails memberDetails, ClassDetails declaringClass);
3536

36-
void addProperty(Property prop, MemberDetails memberDetails, AnnotatedColumns columns, ClassDetails declaringClass);
37+
void addProperty(Property prop, MemberDetails memberDetails, @Nullable AnnotatedColumns columns, ClassDetails declaringClass);
3738

3839
KeyValue getIdentifier();
3940

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static PropertyHolder buildPropertyHolder(
4747
*
4848
* @return PropertyHolder
4949
*/
50-
public static ComponentPropertyHolder buildPropertyHolder(
50+
public static PropertyHolder buildPropertyHolder(
5151
Component component,
5252
String path,
5353
PropertyData inferredData,

0 commit comments

Comments
 (0)