HHH-10275 - Fix Inverse foreign key for many-to-many set is nullable

This commit is contained in:
Andrea Boriero 2015-11-16 17:29:32 +00:00
parent e752a78777
commit b421040359
10 changed files with 37 additions and 22 deletions

View File

@ -319,7 +319,7 @@ public class MultipleHiLoPerTableGenerator implements PersistentIdentifierGenera
table = namespace.createTable( qualifiedTableName.getObjectName(), false );
// todo : note sure the best solution here. do we add the columns if missing? other?
table.setPrimaryKey( new PrimaryKey() );
table.setPrimaryKey( new PrimaryKey( table ) );
final Column pkColumn = new ExportableColumn(
database,

View File

@ -688,8 +688,7 @@ public class TableGenerator implements PersistentIdentifierGenerator, Configurab
table.addColumn( segmentColumn );
// lol
table.setPrimaryKey( new PrimaryKey() );
table.getPrimaryKey().setTable( table );
table.setPrimaryKey( new PrimaryKey( table ) );
table.getPrimaryKey().addColumn( segmentColumn );
final Column valueColumn = new ExportableColumn(

View File

@ -191,7 +191,7 @@ public abstract class Constraint implements RelationalModel, Exportable, Seriali
return null;
}
public List getColumns() {
public List<Column> getColumns() {
return columns;
}

View File

@ -35,7 +35,7 @@ public abstract class IdentifierCollection extends Collection {
void createPrimaryKey() {
if ( !isOneToMany() ) {
PrimaryKey pk = new PrimaryKey();
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
pk.addColumns( getIdentifier().getColumnIterator() );
getCollectionTable().setPrimaryKey(pk);
}

View File

@ -39,7 +39,7 @@ public abstract class IndexedCollection extends Collection {
void createPrimaryKey() {
if ( !isOneToMany() ) {
PrimaryKey pk = new PrimaryKey();
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
pk.addColumns( getKey().getColumnIterator() );
// index should be last column listed

View File

@ -91,8 +91,7 @@ public class Join implements AttributeContainer, Serializable {
public void createPrimaryKey() {
//Primary key constraint
PrimaryKey pk = new PrimaryKey();
pk.setTable(table);
PrimaryKey pk = new PrimaryKey( table );
pk.setName( PK_ALIAS.toAliasString( table.getName() ) );
table.setPrimaryKey(pk);

View File

@ -356,9 +356,8 @@ public abstract class PersistentClass implements AttributeContainer, Serializabl
public void createPrimaryKey() {
//Primary key constraint
PrimaryKey pk = new PrimaryKey();
Table table = getTable();
pk.setTable( table );
final Table table = getTable();
PrimaryKey pk = new PrimaryKey( table );
pk.setName( PK_ALIAS.toAliasString( table.getName() ) );
table.setPrimaryKey( pk );

View File

@ -21,18 +21,23 @@ import org.jboss.logging.Logger;
public class PrimaryKey extends Constraint {
private static final Logger log = Logger.getLogger( PrimaryKey.class );
public PrimaryKey(Table table){
setTable( table );
}
@Override
public void addColumn(Column column) {
if ( column.isNullable() ) {
if ( log.isDebugEnabled() ) {
final String columnName = column.getCanonicalName();
final Iterator<Column> columnIterator = getTable().getColumnIterator();
while ( columnIterator.hasNext() ) {
final Column next = columnIterator.next();
if ( next.getCanonicalName().equals( column.getCanonicalName() ) ) {
next.setNullable( false );
log.debugf(
"Forcing column [%s] to be non-null as it is part of the primary key for table [%s]",
columnName,
getTableNameForLogging( column )
column.getCanonicalName(),
getTable().getNameIdentifier().getCanonicalName()
);
}
column.setNullable( false );
}
super.addColumn( column );
}

View File

@ -60,7 +60,7 @@ public class Set extends Collection {
void createPrimaryKey() {
if ( !isOneToMany() ) {
PrimaryKey pk = new PrimaryKey();
PrimaryKey pk = new PrimaryKey( getCollectionTable() );
pk.addColumns( getKey().getColumnIterator() );
Iterator iter = getElement().getColumnIterator();
while ( iter.hasNext() ) {

View File

@ -27,7 +27,6 @@ import org.hibernate.engine.jdbc.env.spi.QualifiedObjectNameFormatter;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.tool.hbm2ddl.ColumnMetadata;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;
import org.hibernate.tool.hbm2ddl.TableMetadata;
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;
@ -41,6 +40,8 @@ import org.jboss.logging.Logger;
*/
@SuppressWarnings("unchecked")
public class Table implements RelationalModel, Serializable, Exportable {
private static final Logger log = Logger.getLogger( Table.class );
private Identifier catalog;
private Identifier schema;
private Identifier name;
@ -254,8 +255,20 @@ public class Table implements RelationalModel, Serializable, Exportable {
public void addColumn(Column column) {
Column old = getColumn( column );
if ( old == null ) {
columns.put( column.getCanonicalName(), column );
column.uniqueInteger = columns.size();
if ( primaryKey != null ) {
for ( Column c : primaryKey.getColumns() ) {
if ( c.getCanonicalName().equals( column.getCanonicalName() ) ) {
column.setNullable( false );
log.debugf(
"Forcing column [%s] to be non-null as it is part of the primary key for table [%s]",
column.getCanonicalName(),
getNameIdentifier().getCanonicalName()
);
}
}
}
this.columns.put( column.getCanonicalName(), column );
column.uniqueInteger = this.columns.size();
}
else {
column.uniqueInteger = old.uniqueInteger;
@ -489,7 +502,7 @@ public class Table implements RelationalModel, Serializable, Exportable {
}
if ( results.isEmpty() ) {
Logger.getLogger( SchemaUpdate.class ).debugf( "No alter strings for table : %s", getQuotedName() );
log.debugf( "No alter strings for table : %s", getQuotedName() );
}
return results.iterator();