From ceb4d060da11e28a9a2a7e085522c0b6cefc6732 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Sat, 20 Apr 2024 15:10:40 +0200 Subject: [PATCH] HHH-17965 @ElementCollection with @CollectionTable throws o.h.MappingException Foreign key must have same number of columns as the referenced primary key --- .../main/java/org/hibernate/mapping/Set.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/mapping/Set.java b/hibernate-core/src/main/java/org/hibernate/mapping/Set.java index f818ca9b662e..c311ea59ffe8 100644 --- a/hibernate-core/src/main/java/org/hibernate/mapping/Set.java +++ b/hibernate-core/src/main/java/org/hibernate/mapping/Set.java @@ -84,27 +84,27 @@ void createPrimaryKey() { PrimaryKey pk = collectionTable.getPrimaryKey(); if ( pk == null ) { pk = new PrimaryKey( getCollectionTable() ); - } - pk.addColumns( getKey() ); - for ( Selectable selectable : getElement().getSelectables() ) { - if ( selectable instanceof Column ) { - Column col = (Column) selectable; - if ( !col.isNullable() ) { - pk.addColumn( col ); - } - else { - return; + pk.addColumns( getKey() ); + for ( Selectable selectable : getElement().getSelectables() ) { + if ( selectable instanceof Column ) { + Column col = (Column) selectable; + if ( !col.isNullable() ) { + pk.addColumn( col ); + } + else { + return; + } } } + if ( pk.getColumnSpan() != getKey().getColumnSpan() ) { + collectionTable.setPrimaryKey( pk ); + } +// else { + //for backward compatibility, allow a set with no not-null + //element columns, using all columns in the row locator SQL + //TODO: create an implicit not null constraint on all cols? +// } } - if ( pk.getColumnSpan() != getKey().getColumnSpan() ) { - collectionTable.setPrimaryKey( pk ); - } -// else { - //for backward compatibility, allow a set with no not-null - //element columns, using all columns in the row locator SQL - //TODO: create an implicit not null constraint on all cols? -// } } // else { //create an index on the key columns??