HHH-15759 Entity with @CollectionTable and entity with relation to element collection table, cause generate wrong FK, if first entity key field lexicographically after collection element.

This commit is contained in:
Andrea Boriero 2022-12-12 11:37:02 +01:00 committed by Andrea Boriero
parent d95bb9c40e
commit 9341df0b8b
1 changed files with 6 additions and 2 deletions

View File

@ -80,7 +80,11 @@ public class Set extends Collection {
void createPrimaryKey() {
if ( !isOneToMany() ) {
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
final Table collectionTable = getCollectionTable();
PrimaryKey pk = collectionTable.getPrimaryKey();
if ( pk == null ) {
pk = new PrimaryKey( getCollectionTable() );
}
pk.addColumns( getKey() );
for ( Selectable selectable : getElement().getSelectables() ) {
if ( selectable instanceof Column ) {
@ -94,7 +98,7 @@ public class Set extends Collection {
}
}
if ( pk.getColumnSpan() != getKey().getColumnSpan() ) {
getCollectionTable().setPrimaryKey( pk );
collectionTable.setPrimaryKey( pk );
}
// else {
//for backward compatibility, allow a set with no not-null