Skip to content

Commit 53bfb21

Browse files
committed
HHH-19299 - <element-collection/> with LIST classification interpreted as BAG
1 parent ed52201 commit 53bfb21

File tree

3 files changed

+43
-20
lines changed

3 files changed

+43
-20
lines changed

hibernate-core/src/main/java/org/hibernate/boot/models/xml/internal/attr/CommonPluralAttributeProcessing.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ public static void applyPluralAttributeStructure(
201201
private static FetchMode interpretFetchMode(JaxbPluralFetchModeImpl fetchMode) {
202202
return switch ( fetchMode ) {
203203
case JOIN -> FetchMode.JOIN;
204-
case SELECT -> FetchMode.SELECT;
205-
case SUBSELECT -> FetchMode.SELECT;
204+
case SELECT, SUBSELECT -> FetchMode.SELECT;
206205
};
207206
}
208207

@@ -211,15 +210,16 @@ private static void applyOrderColumn(
211210
MutableMemberDetails memberDetails,
212211
XmlDocumentContext xmlDocumentContext) {
213212
final JaxbOrderColumnImpl jaxbOrderColumn = jaxbPluralAttribute.getOrderColumn();
214-
if ( jaxbOrderColumn == null ) {
215-
return;
216-
}
217-
218-
final OrderColumnJpaAnnotation orderColumnAnn = (OrderColumnJpaAnnotation) memberDetails.applyAnnotationUsage(
219-
JpaAnnotations.ORDER_COLUMN,
220-
xmlDocumentContext.getModelBuildingContext()
221-
);
213+
if ( jaxbOrderColumn != null
214+
|| jaxbPluralAttribute.getClassification() == LimitedCollectionClassification.LIST ) {
215+
final OrderColumnJpaAnnotation orderColumnAnn = (OrderColumnJpaAnnotation) memberDetails.applyAnnotationUsage(
216+
JpaAnnotations.ORDER_COLUMN,
217+
xmlDocumentContext.getModelBuildingContext()
218+
);
222219

223-
orderColumnAnn.apply( jaxbOrderColumn, xmlDocumentContext );
220+
if ( jaxbOrderColumn != null ) {
221+
orderColumnAnn.apply( jaxbOrderColumn, xmlDocumentContext );
222+
}
223+
}
224224
}
225225
}

hibernate-core/src/test/java/org/hibernate/orm/test/boot/models/hbm/collections/list/ListTests.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.hibernate.mapping.BasicValue;
88
import org.hibernate.mapping.Component;
9+
import org.hibernate.mapping.KeyValue;
910
import org.hibernate.mapping.List;
1011
import org.hibernate.mapping.ManyToOne;
1112
import org.hibernate.mapping.PersistentClass;
@@ -29,6 +30,8 @@ void testXml(DomainModelScope domainModelScope) {
2930
final PersistentClass rootBinding = domainModelScope.getDomainModel().getEntityBinding( Root.class.getName() );
3031
validateTags( rootBinding.getProperty( "tags" ) );
3132
validateCategories( rootBinding.getProperty( "categories" ) );
33+
validateAdmins( rootBinding.getProperty( "admins" ) );
34+
validateAdmins2( rootBinding.getProperty( "admins2" ) );
3235
}
3336

3437
private void validateTags(Property tags) {
@@ -58,9 +61,6 @@ private void validateCategories(Property categories) {
5861
else if ( "owner".equals( subProperty.getName() ) ) {
5962
validateCategoryOwner( subProperty );
6063
}
61-
else if ( "admins".equals( subProperty.getName() ) ) {
62-
validateAdmins( subProperty );
63-
}
6464
else {
6565
fail( "Unexpected Category property :" + subProperty.getName() );
6666
}
@@ -80,16 +80,41 @@ private void validateCategoryOwner(Property owenerProperty) {
8080

8181
}
8282

83-
private void validateAdmins(Property adminsProperty) {
84-
assertThat( adminsProperty.getColumns() ).hasSize( 1 );
85-
assertThat( adminsProperty.getColumns().get( 0 ).getName() ).isEqualTo( "root_fk" );
83+
private void validateAdmins(Property property) {
84+
// mapped as many-to-many
85+
assertThat( property.getColumns() ).isEmpty();
8686

87-
final List listValue = (List) adminsProperty.getValue();
87+
final List listValue = (List) property.getValue();
8888
assertThat( listValue.getCollectionTable().getName() ).isEqualTo( "root_admins" );
8989

90+
final KeyValue foreignKey = listValue.getKey();
91+
assertThat( foreignKey.getColumns() ).hasSize( 1 );
92+
assertThat( foreignKey.getColumns().get( 0 ).getName() ).isEqualTo( "root_fk" );
93+
94+
final BasicValue indexValue = (BasicValue) listValue.getIndex();
95+
assertThat( indexValue.getColumns() ).hasSize( 1 );
96+
97+
final ManyToOne element = (ManyToOne) listValue.getElement();
98+
assertThat( element.getReferencedEntityName() ).isEqualTo( User.class.getName() );
99+
}
100+
101+
private void validateAdmins2(Property property) {
102+
// mapped as one-to-many
103+
assertThat( property.getColumns() ).isEmpty();
104+
105+
final List listValue = (List) property.getValue();
106+
assertThat( listValue.getColumns() ).isEmpty();
107+
assertThat( listValue.getCollectionTable().getName() ).isEqualTo( "root_admins2" );
108+
109+
// key
110+
final KeyValue foreignKey = listValue.getKey();
111+
assertThat( foreignKey.getColumns() ).hasSize( 1 );
112+
assertThat( foreignKey.getColumns().get( 0 ).getName() ).isEqualTo( "root_fk" );
113+
90114
final BasicValue indexValue = (BasicValue) listValue.getIndex();
91115
assertThat( indexValue.getColumns() ).hasSize( 1 );
92116

93117
final ManyToOne element = (ManyToOne) listValue.getElement();
118+
assertThat( element.getReferencedEntityName() ).isEqualTo( User.class.getName() );
94119
}
95120
}

hibernate-core/src/test/resources/mappings/models/hbm/list/mapping.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,13 @@
2222
</one-to-many>
2323

2424
<many-to-many name="admins" classification="LIST" target-entity="User">
25-
<order-column/>
2625
<join-table name="root_admins">
2726
<join-column name="root_fk"/>
2827
<inverse-join-column name="user_fk"/>
2928
</join-table>
3029
</many-to-many>
3130

3231
<element-collection name="tags" classification="LIST" target-class="java.lang.String">
33-
<order-column/>
3432
<column name="txt"/>
3533
<collection-table>
3634
<join-column name="root_fk"/>

0 commit comments

Comments
 (0)