Skip to content

Commit c6ba656

Browse files
committed
HHH-19542: Feed-back from Gavin King
1 parent 78793c3 commit c6ba656

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

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

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

7+
import java.util.ArrayList;
78
import java.util.HashMap;
9+
import java.util.List;
810
import java.util.Map;
911
import java.util.Objects;
10-
import java.util.concurrent.atomic.AtomicReference;
1112

1213
import org.hibernate.AnnotationException;
1314
import org.hibernate.boot.spi.MetadataBuildingContext;
1415
import org.hibernate.boot.spi.PropertyData;
16+
import org.hibernate.internal.util.StringHelper;
1517
import org.hibernate.mapping.AggregateColumn;
1618
import org.hibernate.mapping.Component;
1719
import org.hibernate.mapping.Join;
@@ -31,7 +33,6 @@
3133
import static org.hibernate.boot.model.internal.ClassPropertyHolder.handleGenericComponentProperty;
3234
import static org.hibernate.boot.model.internal.PropertyBinder.hasIdAnnotation;
3335
import static org.hibernate.internal.util.StringHelper.isEmpty;
34-
import static org.hibernate.internal.util.StringHelper.nullIfEmpty;
3536
import static org.hibernate.internal.util.StringHelper.qualifyConditionally;
3637
import static org.hibernate.spi.NavigablePath.IDENTIFIER_MAPPER_PROPERTY;
3738

@@ -71,7 +72,7 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
7172

7273
private final String embeddedAttributeName;
7374
private final Map<String,AttributeConversionInfo> attributeConversionInfoMap;
74-
private final AtomicReference<AnnotatedColumn> annotatedColumn;
75+
private final List<AnnotatedColumn> annotatedColumns;
7576

7677
public ComponentPropertyHolder(
7778
Component component,
@@ -100,9 +101,9 @@ public ComponentPropertyHolder(
100101
}
101102

102103
if ( parent instanceof ComponentPropertyHolder parentHolder ) {
103-
this.annotatedColumn = parentHolder.annotatedColumn;
104+
this.annotatedColumns = parentHolder.annotatedColumns;
104105
} else {
105-
this.annotatedColumn = new AtomicReference<>();
106+
this.annotatedColumns = new ArrayList<>();
106107
}
107108
}
108109

@@ -236,7 +237,7 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
236237
// if not, change the component table if no properties are set
237238
// if a property is set already the core cannot support that
238239
final Table table = property.getValue().getTable();
239-
if ( !table.equals( getTable() ) || !columnTableIsConsistent( columns ) ) {
240+
if ( !table.equals( getTable() ) ) {
240241
if ( component.getPropertySpan() == 0 ) {
241242
component.setTable( table );
242243
}
@@ -248,21 +249,28 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
248249
);
249250
}
250251
}
252+
if ( columns != null ) {
253+
annotatedColumns.addAll( columns.getColumns() );
254+
}
251255
addProperty( property, attributeMemberDetails, declaringClass );
252256
}
253257

254-
private boolean columnTableIsConsistent(AnnotatedColumns columns) {
255-
if ( columns != null ) {
256-
for ( AnnotatedColumn current : columns.getColumns() ) {
257-
final AnnotatedColumn previous = annotatedColumn.getAndSet( current );
258-
if ( previous != null && !Objects.equals(
259-
nullIfEmpty( current.getExplicitTableName() ),
260-
nullIfEmpty( previous.getExplicitTableName() ) ) ) {
261-
return false;
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+
);
262271
}
263272
}
264273
}
265-
return true;
266274
}
267275

268276
@Override

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

Lines changed: 3 additions & 1 deletion
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 PropertyHolder subholder = buildPropertyHolder(
458+
final ComponentPropertyHolder subholder = buildPropertyHolder(
459459
component,
460460
subpath,
461461
inferredData,
@@ -579,6 +579,8 @@ else if ( member.hasDirectAnnotationUsage( GeneratedValue.class ) ) {
579579
}
580580
}
581581

582+
subholder.checkPropertyConsistency();
583+
582584
if ( compositeUserType != null ) {
583585
processCompositeUserType( component, compositeUserType );
584586
}

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 PropertyHolder buildPropertyHolder(
50+
public static ComponentPropertyHolder buildPropertyHolder(
5151
Component component,
5252
String path,
5353
PropertyData inferredData,

0 commit comments

Comments
 (0)