44 */
55package org .hibernate .boot .model .internal ;
66
7- import java .util .ArrayList ;
87import java .util .HashMap ;
9- import java .util .List ;
108import java .util .Map ;
11- import java .util .Objects ;
129
1310import org .hibernate .AnnotationException ;
1411import org .hibernate .boot .spi .MetadataBuildingContext ;
@@ -71,7 +68,6 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
7168
7269 private final String embeddedAttributeName ;
7370 private final Map <String ,AttributeConversionInfo > attributeConversionInfoMap ;
74- private final List <AnnotatedColumn > annotatedColumns ;
7571
7672 public ComponentPropertyHolder (
7773 Component component ,
@@ -98,12 +94,6 @@ public ComponentPropertyHolder(
9894 this .embeddedAttributeName = "" ;
9995 this .attributeConversionInfoMap = processAttributeConversions ( inferredData .getClassOrElementType () );
10096 }
101-
102- if ( parent instanceof ComponentPropertyHolder componentHolder ) {
103- this .annotatedColumns = componentHolder .annotatedColumns ;
104- } else {
105- this .annotatedColumns = new ArrayList <>();
106- }
10797 }
10898
10999 /**
@@ -229,30 +219,14 @@ public String getEntityName() {
229219 return component .getComponentClassName ();
230220 }
231221
232- public void checkPropertyConsistency () {
233- if ( annotatedColumns .size () > 1 ) {
234- for ( int currentIndex = 1 ; currentIndex < annotatedColumns .size (); currentIndex ++ ) {
235- final AnnotatedColumn current = annotatedColumns .get ( currentIndex );
236- final AnnotatedColumn previous = annotatedColumns .get ( currentIndex - 1 );
237- if ( !Objects .equals ( current .getExplicitTableName (), previous .getExplicitTableName () ) ) {
238- throw new AnnotationException (
239- "Embeddable class '" + component .getComponentClassName ()
240- + "' has properties mapped to two different tables"
241- + " (all properties of the embeddable class must map to the same table)"
242- );
243- }
244- }
245- }
246- }
247-
248222 @ Override
249223 public void addProperty (Property property , MemberDetails attributeMemberDetails , AnnotatedColumns columns , ClassDetails declaringClass ) {
250224 //AnnotatedColumns.checkPropertyConsistency( ); //already called earlier
251225 // Check table matches between the component and the columns
252226 // if not, change the component table if no properties are set
253227 // if a property is set already the core cannot support that
254228 final Table table = property .getValue ().getTable ();
255- if ( !table .equals ( getTable () ) ) {
229+ if ( !table .equals ( getTable () ) || ! columnTableIsExplicit ( table , columns ) ) {
256230 if ( component .getPropertySpan () == 0 ) {
257231 component .setTable ( table );
258232 }
@@ -264,21 +238,19 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
264238 );
265239 }
266240 }
267- // In the case of a nested component, AnnotatedColumns.checkPropertyConsistency()
268- // is not called since we rely on the underlying value, we build a synthetic annotated
269- // columns to check the consistency after all properties are set.
241+ addProperty ( property , attributeMemberDetails , declaringClass );
242+ }
243+
244+ private boolean columnTableIsExplicit (Table table , AnnotatedColumns columns ) {
270245 if ( columns != null ) {
271- for ( AnnotatedColumn column : columns .getColumns () ) {
272- // The following has to be added otherwise every single
273- // property must be annotated with @Column
274- // Can be removed in version where the binder is stricter
275- if ( column .getExplicitTableName () == null ) {
276- column .setExplicitTableName ( "" );
246+ for ( AnnotatedColumn current : columns .getColumns () ) {
247+ final String explicitTableName = current .getExplicitTableName ();
248+ if ( columns .isSecondary () && !table .getName ().equals ( explicitTableName ) ) {
249+ return false ;
277250 }
278- this .annotatedColumns .add ( column );
279251 }
280252 }
281- addProperty ( property , attributeMemberDetails , declaringClass ) ;
253+ return true ;
282254 }
283255
284256 @ Override
0 commit comments