Skip to content

Commit b421040

Browse files
committed
HHH-10275 - Fix Inverse foreign key for many-to-many set is nullable
1 parent e752a78 commit b421040

File tree

10 files changed

+37
-22
lines changed

10 files changed

+37
-22
lines changed

hibernate-core/src/main/java/org/hibernate/id/MultipleHiLoPerTableGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public void registerExportables(Database database) {
319319
table = namespace.createTable( qualifiedTableName.getObjectName(), false );
320320

321321
// todo : note sure the best solution here. do we add the columns if missing? other?
322-
table.setPrimaryKey( new PrimaryKey() );
322+
table.setPrimaryKey( new PrimaryKey( table ) );
323323

324324
final Column pkColumn = new ExportableColumn(
325325
database,

hibernate-core/src/main/java/org/hibernate/id/enhanced/TableGenerator.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,7 @@ public void registerExportables(Database database) {
688688
table.addColumn( segmentColumn );
689689

690690
// lol
691-
table.setPrimaryKey( new PrimaryKey() );
692-
table.getPrimaryKey().setTable( table );
691+
table.setPrimaryKey( new PrimaryKey( table ) );
693692
table.getPrimaryKey().addColumn( segmentColumn );
694693

695694
final Column valueColumn = new ExportableColumn(

hibernate-core/src/main/java/org/hibernate/mapping/Constraint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog,
191191
return null;
192192
}
193193

194-
public List getColumns() {
194+
public List<Column> getColumns() {
195195
return columns;
196196
}
197197

hibernate-core/src/main/java/org/hibernate/mapping/IdentifierCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final boolean isIdentified() {
3535

3636
void createPrimaryKey() {
3737
if ( !isOneToMany() ) {
38-
PrimaryKey pk = new PrimaryKey();
38+
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
3939
pk.addColumns( getIdentifier().getColumnIterator() );
4040
getCollectionTable().setPrimaryKey(pk);
4141
}

hibernate-core/src/main/java/org/hibernate/mapping/IndexedCollection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final boolean isIndexed() {
3939

4040
void createPrimaryKey() {
4141
if ( !isOneToMany() ) {
42-
PrimaryKey pk = new PrimaryKey();
42+
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
4343
pk.addColumns( getKey().getColumnIterator() );
4444

4545
// index should be last column listed

hibernate-core/src/main/java/org/hibernate/mapping/Join.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ public void createForeignKey() {
9191

9292
public void createPrimaryKey() {
9393
//Primary key constraint
94-
PrimaryKey pk = new PrimaryKey();
95-
pk.setTable(table);
94+
PrimaryKey pk = new PrimaryKey( table );
9695
pk.setName( PK_ALIAS.toAliasString( table.getName() ) );
9796
table.setPrimaryKey(pk);
9897

hibernate-core/src/main/java/org/hibernate/mapping/PersistentClass.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,8 @@ public void setEntityName(String entityName) {
356356

357357
public void createPrimaryKey() {
358358
//Primary key constraint
359-
PrimaryKey pk = new PrimaryKey();
360-
Table table = getTable();
361-
pk.setTable( table );
359+
final Table table = getTable();
360+
PrimaryKey pk = new PrimaryKey( table );
362361
pk.setName( PK_ALIAS.toAliasString( table.getName() ) );
363362
table.setPrimaryKey( pk );
364363

hibernate-core/src/main/java/org/hibernate/mapping/PrimaryKey.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,23 @@
2121
public class PrimaryKey extends Constraint {
2222
private static final Logger log = Logger.getLogger( PrimaryKey.class );
2323

24+
public PrimaryKey(Table table){
25+
setTable( table );
26+
}
27+
2428
@Override
2529
public void addColumn(Column column) {
26-
if ( column.isNullable() ) {
27-
if ( log.isDebugEnabled() ) {
28-
final String columnName = column.getCanonicalName();
30+
final Iterator<Column> columnIterator = getTable().getColumnIterator();
31+
while ( columnIterator.hasNext() ) {
32+
final Column next = columnIterator.next();
33+
if ( next.getCanonicalName().equals( column.getCanonicalName() ) ) {
34+
next.setNullable( false );
2935
log.debugf(
3036
"Forcing column [%s] to be non-null as it is part of the primary key for table [%s]",
31-
columnName,
32-
getTableNameForLogging( column )
37+
column.getCanonicalName(),
38+
getTable().getNameIdentifier().getCanonicalName()
3339
);
3440
}
35-
column.setNullable( false );
3641
}
3742
super.addColumn( column );
3843
}

hibernate-core/src/main/java/org/hibernate/mapping/Set.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ else if ( hasOrder() ) {
6060

6161
void createPrimaryKey() {
6262
if ( !isOneToMany() ) {
63-
PrimaryKey pk = new PrimaryKey();
63+
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
6464
pk.addColumns( getKey().getColumnIterator() );
6565
Iterator iter = getElement().getColumnIterator();
6666
while ( iter.hasNext() ) {

hibernate-core/src/main/java/org/hibernate/mapping/Table.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.hibernate.engine.spi.Mapping;
2828
import org.hibernate.internal.util.StringHelper;
2929
import org.hibernate.tool.hbm2ddl.ColumnMetadata;
30-
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
3130
import org.hibernate.tool.hbm2ddl.TableMetadata;
3231
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
3332
import org.hibernate.tool.schema.extract.spi.TableInformation;
@@ -41,6 +40,8 @@
4140
*/
4241
@SuppressWarnings("unchecked")
4342
public class Table implements RelationalModel, Serializable, Exportable {
43+
private static final Logger log = Logger.getLogger( Table.class );
44+
4445
private Identifier catalog;
4546
private Identifier schema;
4647
private Identifier name;
@@ -254,8 +255,20 @@ public Column getColumn(int n) {
254255
public void addColumn(Column column) {
255256
Column old = getColumn( column );
256257
if ( old == null ) {
257-
columns.put( column.getCanonicalName(), column );
258-
column.uniqueInteger = columns.size();
258+
if ( primaryKey != null ) {
259+
for ( Column c : primaryKey.getColumns() ) {
260+
if ( c.getCanonicalName().equals( column.getCanonicalName() ) ) {
261+
column.setNullable( false );
262+
log.debugf(
263+
"Forcing column [%s] to be non-null as it is part of the primary key for table [%s]",
264+
column.getCanonicalName(),
265+
getNameIdentifier().getCanonicalName()
266+
);
267+
}
268+
}
269+
}
270+
this.columns.put( column.getCanonicalName(), column );
271+
column.uniqueInteger = this.columns.size();
259272
}
260273
else {
261274
column.uniqueInteger = old.uniqueInteger;
@@ -489,7 +502,7 @@ public Iterator sqlAlterStrings(
489502
}
490503

491504
if ( results.isEmpty() ) {
492-
Logger.getLogger( SchemaUpdate.class ).debugf( "No alter strings for table : %s", getQuotedName() );
505+
log.debugf( "No alter strings for table : %s", getQuotedName() );
493506
}
494507

495508
return results.iterator();

0 commit comments

Comments
 (0)