Skip to content

Commit 0b1c287

Browse files
committed
HHH-19542 Ensure columns in embeddables default to correct table
1 parent 162b935 commit 0b1c287

File tree

4 files changed

+20
-39
lines changed

4 files changed

+20
-39
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/ComponentPropertyHolder.java

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
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

1310
import org.hibernate.AnnotationException;
1411
import org.hibernate.boot.spi.MetadataBuildingContext;
1512
import org.hibernate.boot.spi.PropertyData;
16-
import org.hibernate.internal.util.StringHelper;
1713
import org.hibernate.mapping.AggregateColumn;
1814
import org.hibernate.mapping.Component;
1915
import org.hibernate.mapping.Join;
@@ -72,7 +68,6 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
7268

7369
private final String embeddedAttributeName;
7470
private final Map<String,AttributeConversionInfo> attributeConversionInfoMap;
75-
private final List<AnnotatedColumn> annotatedColumns;
7671

7772
public ComponentPropertyHolder(
7873
Component component,
@@ -99,12 +94,6 @@ public ComponentPropertyHolder(
9994
this.embeddedAttributeName = "";
10095
this.attributeConversionInfoMap = processAttributeConversions( inferredData.getClassOrElementType() );
10196
}
102-
103-
if ( parent instanceof ComponentPropertyHolder parentHolder ) {
104-
this.annotatedColumns = parentHolder.annotatedColumns;
105-
} else {
106-
this.annotatedColumns = new ArrayList<>();
107-
}
10897
}
10998

11099
/**
@@ -236,7 +225,12 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
236225
// Check table matches between the component and the columns
237226
// if not, change the component table if no properties are set
238227
// if a property is set already the core cannot support that
239-
final Table table = property.getValue().getTable();
228+
assert property.getValue().getTable() == columns.getTable();
229+
setTable( property.getValue().getTable() );
230+
addProperty( property, attributeMemberDetails, declaringClass );
231+
}
232+
233+
void setTable(Table table) {
240234
if ( !table.equals( getTable() ) ) {
241235
if ( component.getPropertySpan() == 0 ) {
242236
component.setTable( table );
@@ -248,27 +242,8 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
248242
+ " (all properties of the embeddable class must map to the same table)"
249243
);
250244
}
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-
}
245+
if ( parent instanceof ComponentPropertyHolder parentComponentHolder ) {
246+
parentComponentHolder.setTable( table );
272247
}
273248
}
274249
}

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/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)