Skip to content

Commit 9bfabbd

Browse files
committed
HHH-19542 Ensure columns in embeddables default to correct table
1 parent 381e323 commit 9bfabbd

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
@@ -106,7 +106,7 @@ public boolean isSecondary() {
106106
final String explicitTableName = firstColumn.getExplicitTableName();
107107
//note: checkPropertyConsistency() is responsible for ensuring they all have the same table name
108108
return isNotEmpty( explicitTableName )
109-
&& !getPropertyHolder().getTable().getName().equals( explicitTableName );
109+
&& !getOwnerTable().getName().equals( explicitTableName );
110110
}
111111

112112
/**
@@ -125,10 +125,18 @@ public Table getTable() {
125125
// all the columns have to be mapped to the same table
126126
// even though at the annotation level it looks like
127127
// they could each specify a different table
128-
return isSecondary() ? getJoin().getTable() : getPropertyHolder().getTable();
128+
return isSecondary() ? getJoin().getTable() : getOwnerTable();
129129
}
130130
}
131131

132+
private Table getOwnerTable() {
133+
PropertyHolder holder = getPropertyHolder();
134+
while ( holder instanceof ComponentPropertyHolder ) {
135+
holder = ( (ComponentPropertyHolder) holder ).parent;
136+
}
137+
return holder.getTable();
138+
}
139+
132140
public void setTable(Table table) {
133141
this.table = table;
134142
}

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
@@ -6,18 +6,14 @@
66
*/
77
package org.hibernate.boot.model.internal;
88

9-
import java.util.ArrayList;
109
import java.util.HashMap;
11-
import java.util.List;
1210
import java.util.Map;
13-
import java.util.Objects;
1411

1512
import org.hibernate.AnnotationException;
1613
import org.hibernate.annotations.common.reflection.XClass;
1714
import org.hibernate.annotations.common.reflection.XProperty;
1815
import org.hibernate.boot.spi.MetadataBuildingContext;
1916
import org.hibernate.boot.spi.PropertyData;
20-
import org.hibernate.internal.util.StringHelper;
2117
import org.hibernate.mapping.AggregateColumn;
2218
import org.hibernate.mapping.Component;
2319
import org.hibernate.mapping.Join;
@@ -78,7 +74,6 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
7874

7975
private final String embeddedAttributeName;
8076
private final Map<String,AttributeConversionInfo> attributeConversionInfoMap;
81-
private final List<AnnotatedColumn> annotatedColumns;
8277

8378
public ComponentPropertyHolder(
8479
Component component,
@@ -105,12 +100,6 @@ public ComponentPropertyHolder(
105100
this.embeddedAttributeName = "";
106101
this.attributeConversionInfoMap = processAttributeConversions( inferredData.getClassOrPluralElement() );
107102
}
108-
109-
if ( parent instanceof ComponentPropertyHolder ) {
110-
this.annotatedColumns = ( (ComponentPropertyHolder) parent ).annotatedColumns;
111-
} else {
112-
this.annotatedColumns = new ArrayList<>();
113-
}
114103
}
115104

116105
/**
@@ -281,7 +270,12 @@ public void addProperty(Property property, AnnotatedColumns columns, XClass decl
281270
// Check table matches between the component and the columns
282271
// if not, change the component table if no properties are set
283272
// if a property is set already the core cannot support that
284-
final Table table = property.getValue().getTable();
273+
assert columns == null || property.getValue().getTable() == columns.getTable();
274+
setTable( property.getValue().getTable() );
275+
addProperty( property, declaringClass );
276+
}
277+
278+
private void setTable(Table table) {
285279
if ( !table.equals( getTable() ) ) {
286280
if ( component.getPropertySpan() == 0 ) {
287281
component.setTable( table );
@@ -293,27 +287,8 @@ public void addProperty(Property property, AnnotatedColumns columns, XClass decl
293287
+ " (all properties of the embeddable class must map to the same table)"
294288
);
295289
}
296-
}
297-
if ( columns != null ) {
298-
annotatedColumns.addAll( columns.getColumns() );
299-
}
300-
addProperty( property, declaringClass );
301-
}
302-
303-
public void checkPropertyConsistency() {
304-
if ( annotatedColumns.size() > 1 ) {
305-
for ( int currentIndex = 1; currentIndex < annotatedColumns.size(); currentIndex++ ) {
306-
final AnnotatedColumn current = annotatedColumns.get( currentIndex );
307-
final AnnotatedColumn previous = annotatedColumns.get( currentIndex - 1 );
308-
if ( !Objects.equals(
309-
StringHelper.nullIfEmpty( current.getExplicitTableName() ),
310-
StringHelper.nullIfEmpty( previous.getExplicitTableName() ) ) ) {
311-
throw new AnnotationException(
312-
"Embeddable class '" + component.getComponentClassName()
313-
+ "' has properties mapped to two different tables"
314-
+ " (all properties of the embeddable class must map to the same table)"
315-
);
316-
}
290+
if ( parent instanceof ComponentPropertyHolder ) {
291+
( (ComponentPropertyHolder) parent ).setTable( table );
317292
}
318293
}
319294
}

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
@@ -343,7 +343,7 @@ static Component fillEmbeddable(
343343

344344
final String subpath = getPath( propertyHolder, inferredData );
345345
LOG.tracev( "Binding component with path: {0}", subpath );
346-
final ComponentPropertyHolder subholder = buildPropertyHolder(
346+
final PropertyHolder subholder = buildPropertyHolder(
347347
component,
348348
subpath,
349349
inferredData,
@@ -470,8 +470,6 @@ else if ( member.isAnnotationPresent( GeneratedValue.class ) ) {
470470
}
471471
}
472472

473-
subholder.checkPropertyConsistency();
474-
475473
if ( compositeUserType != null ) {
476474
processCompositeUserType( component, compositeUserType );
477475
}

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
@@ -49,7 +49,7 @@ public static PropertyHolder buildPropertyHolder(
4949
*
5050
* @return PropertyHolder
5151
*/
52-
public static ComponentPropertyHolder buildPropertyHolder(
52+
public static PropertyHolder buildPropertyHolder(
5353
Component component,
5454
String path,
5555
PropertyData inferredData,

0 commit comments

Comments
 (0)