4
4
*/
5
5
package org .hibernate .boot .model .internal ;
6
6
7
+ import java .util .ArrayList ;
7
8
import java .util .HashMap ;
9
+ import java .util .List ;
8
10
import java .util .Map ;
9
11
import java .util .Objects ;
10
- import java .util .concurrent .atomic .AtomicReference ;
11
12
12
13
import org .hibernate .AnnotationException ;
13
14
import org .hibernate .boot .spi .MetadataBuildingContext ;
14
15
import org .hibernate .boot .spi .PropertyData ;
16
+ import org .hibernate .internal .util .StringHelper ;
15
17
import org .hibernate .mapping .AggregateColumn ;
16
18
import org .hibernate .mapping .Component ;
17
19
import org .hibernate .mapping .Join ;
31
33
import static org .hibernate .boot .model .internal .ClassPropertyHolder .handleGenericComponentProperty ;
32
34
import static org .hibernate .boot .model .internal .PropertyBinder .hasIdAnnotation ;
33
35
import static org .hibernate .internal .util .StringHelper .isEmpty ;
34
- import static org .hibernate .internal .util .StringHelper .nullIfEmpty ;
35
36
import static org .hibernate .internal .util .StringHelper .qualifyConditionally ;
36
37
import static org .hibernate .spi .NavigablePath .IDENTIFIER_MAPPER_PROPERTY ;
37
38
@@ -71,7 +72,7 @@ public class ComponentPropertyHolder extends AbstractPropertyHolder {
71
72
72
73
private final String embeddedAttributeName ;
73
74
private final Map <String ,AttributeConversionInfo > attributeConversionInfoMap ;
74
- private final AtomicReference <AnnotatedColumn > annotatedColumn ;
75
+ private final List <AnnotatedColumn > annotatedColumns ;
75
76
76
77
public ComponentPropertyHolder (
77
78
Component component ,
@@ -100,9 +101,9 @@ public ComponentPropertyHolder(
100
101
}
101
102
102
103
if ( parent instanceof ComponentPropertyHolder parentHolder ) {
103
- this .annotatedColumn = parentHolder .annotatedColumn ;
104
+ this .annotatedColumns = parentHolder .annotatedColumns ;
104
105
} else {
105
- this .annotatedColumn = new AtomicReference <>();
106
+ this .annotatedColumns = new ArrayList <>();
106
107
}
107
108
}
108
109
@@ -236,7 +237,7 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
236
237
// if not, change the component table if no properties are set
237
238
// if a property is set already the core cannot support that
238
239
final Table table = property .getValue ().getTable ();
239
- if ( !table .equals ( getTable () ) || ! columnTableIsConsistent ( columns ) ) {
240
+ if ( !table .equals ( getTable () ) ) {
240
241
if ( component .getPropertySpan () == 0 ) {
241
242
component .setTable ( table );
242
243
}
@@ -248,21 +249,28 @@ public void addProperty(Property property, MemberDetails attributeMemberDetails,
248
249
);
249
250
}
250
251
}
252
+ if ( columns != null ) {
253
+ annotatedColumns .addAll ( columns .getColumns () );
254
+ }
251
255
addProperty ( property , attributeMemberDetails , declaringClass );
252
256
}
253
257
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
+ );
262
271
}
263
272
}
264
273
}
265
- return true ;
266
274
}
267
275
268
276
@ Override
0 commit comments