HHH-7830 : union-subclass support

This commit is contained in:
Gail Badner 2013-02-07 09:33:15 -08:00
parent 1634d56c36
commit ba0f96639e
27 changed files with 241 additions and 159 deletions

View File

@ -640,7 +640,7 @@ public class Binder {
entityBinding.getPrimaryTable().getLogicalName().getText()
)
);
joinRelationalValueBindings.add( new RelationalValueBinding( joinColumn, true, false ) );
joinRelationalValueBindings.add( new RelationalValueBinding( entityBinding.getPrimaryTable(), joinColumn, true, false ) );
}
}
else {
@ -667,7 +667,7 @@ public class Binder {
column.setSqlType( joinColumnSource.getSqlType() );
}
}
joinRelationalValueBindings.add( new RelationalValueBinding( column, true, false ) );
joinRelationalValueBindings.add( new RelationalValueBinding( table, column, true, false ) );
}
}
typeHelper.bindJdbcDataType(
@ -680,10 +680,13 @@ public class Binder {
);
// TODO: make the foreign key column the primary key???
final List<Column> targetColumns = determineForeignKeyTargetColumns( entityBinding, secondaryTableSource );
final ForeignKey foreignKey = locateOrCreateForeignKey(
quotedIdentifier( secondaryTableSource.getExplicitForeignKeyName() ),
table,
joinRelationalValueBindings,
determineForeignKeyTargetColumns( entityBinding, secondaryTableSource )
determineForeignKeyTargetTable( entityBinding, targetColumns ),
targetColumns
);
SecondaryTable secondaryTable = new SecondaryTable( table, foreignKey );
secondaryTable.setFetchStyle( secondaryTableSource.getFetchStyle() );
@ -696,11 +699,15 @@ public class Binder {
public ForeignKey locateOrCreateForeignKey(
final String foreignKeyName,
final TableSpecification sourceTable,
final List<RelationalValueBinding> sourceRelationalValueBindings,
final TableSpecification targetTable,
final List<Column> targetColumns) {
return foreignKeyHelper.locateOrCreateForeignKey(
foreignKeyName,
sourceTable,
extractColumnsFromRelationalValueBindings( sourceRelationalValueBindings ),
targetTable,
targetColumns
);
}
@ -1189,14 +1196,17 @@ public class Binder {
attributeBinding.getHibernateTypeDescriptor().getResolvedTypeMapping(),
attributeBinding.getRelationalValueBindings()
);
final List<Column> targetColumns = determineForeignKeyTargetColumns(
attributeBinding.getReferencedEntityBinding(),
attributeSource
);
if ( !attributeBinding.hasDerivedValue() ) {
locateOrCreateForeignKey(
quotedIdentifier( attributeSource.getExplicitForeignKeyName() ),
attributeBinding.getRelationalValueBindings().get( 0 ).getTable(),
attributeBinding.getRelationalValueBindings(),
determineForeignKeyTargetColumns(
attributeBinding.getReferencedEntityBinding(),
attributeSource
)
determineForeignKeyTargetTable( attributeBinding.getReferencedEntityBinding(), targetColumns ),
targetColumns
);
}
return attributeBinding;
@ -1310,13 +1320,16 @@ public class Binder {
.getAttributeBinding()
.getRelationalValueBindings();
final List<Column> targetColumns = determineForeignKeyTargetColumns(
attributeBinding.getReferencedEntityBinding(),
attributeSource
);
locateOrCreateForeignKey(
quotedIdentifier( attributeSource.getExplicitForeignKeyName() ),
foreignKeyRelationalValueBindings.get( 0 ).getTable(),
foreignKeyRelationalValueBindings,
determineForeignKeyTargetColumns(
attributeBinding.getReferencedEntityBinding(),
attributeSource
)
determineForeignKeyTargetTable( attributeBinding.getReferencedEntityBinding(), targetColumns ),
targetColumns
);
}
return attributeBinding;
@ -1810,22 +1823,6 @@ public class Binder {
indexedPluralAttribute.setIndexType( aggregate );
}
private SingularAttributeBinding determineReferencedAttributeBinding(
final ForeignKeyContributingSource foreignKeyContributingSource,
final EntityBinding referencedEntityBinding) {
final JoinColumnResolutionDelegate resolutionDelegate =
foreignKeyContributingSource.getForeignKeyTargetColumnResolutionDelegate();
final JoinColumnResolutionContext resolutionContext = resolutionDelegate == null ? null : new JoinColumnResolutionContextImpl(
referencedEntityBinding
);
return determineReferencedAttributeBinding(
resolutionDelegate,
resolutionContext,
referencedEntityBinding
);
}
private void bindOneToManyCollectionElement(
final OneToManyPluralAttributeElementBinding elementBinding,
final OneToManyPluralAttributeElementSource elementSource,
@ -1882,22 +1879,24 @@ public class Binder {
);
}
final TableSpecification collectionTable = elementBinding.getPluralAttributeBinding().getPluralAttributeKeyBinding().getCollectionTable();
elementBinding.setRelationalValueBindings(
bindValues(
elementBinding.getPluralAttributeBinding().getContainer(),
elementSource,
elementBinding.getPluralAttributeBinding().getAttribute(),
elementBinding.getPluralAttributeBinding().getPluralAttributeKeyBinding().getCollectionTable(),
collectionTable,
namingStrategies,
true
)
);
if ( !elementBinding.getPluralAttributeBinding().getPluralAttributeKeyBinding().isInverse() &&
!elementBinding.hasDerivedValue() ) {
locateOrCreateForeignKey(
quotedIdentifier( elementSource.getExplicitForeignKeyName() ),
collectionTable,
elementBinding.getRelationalValueBindings(),
determineForeignKeyTargetTable( referencedEntityBinding, targetColumns ),
targetColumns
);
}
@ -2257,9 +2256,13 @@ public class Binder {
}
private SingularAttributeBinding determineReferencedAttributeBinding(
final JoinColumnResolutionDelegate resolutionDelegate,
final JoinColumnResolutionContext resolutionContext,
final ForeignKeyContributingSource foreignKeyContributingSource,
final EntityBinding referencedEntityBinding) {
final JoinColumnResolutionDelegate resolutionDelegate =
foreignKeyContributingSource.getForeignKeyTargetColumnResolutionDelegate();
final JoinColumnResolutionContext resolutionContext = resolutionDelegate == null ? null : new JoinColumnResolutionContextImpl(
referencedEntityBinding
);
if ( resolutionDelegate == null ) {
return referencedEntityBinding.getHierarchyDetails().getEntityIdentifier().getAttributeBinding();
}
@ -2300,7 +2303,6 @@ public class Binder {
return (SingularAttributeBinding) referencedAttributeBinding;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ relational binding relates methods
private void bindBasicPluralElementRelationalValues(
final RelationalValueSourceContainer relationalValueSourceContainer,
@ -2398,7 +2400,9 @@ public class Binder {
keyBinding.setRelationalValueBindings( sourceRelationalBindings );
ForeignKey foreignKey = locateOrCreateForeignKey(
quotedIdentifier( keySource.getExplicitForeignKeyName() ),
collectionTable,
sourceRelationalBindings,
determineForeignKeyTargetTable( attributeBinding.getContainer().seekEntityBinding(), targetColumns ),
targetColumns
);
foreignKey.setDeleteRule( keySource.getOnDeleteAction() );
@ -2533,7 +2537,9 @@ public class Binder {
ForeignKey foreignKey = foreignKeyHelper.locateOrCreateForeignKey(
quotedIdentifier( subclassEntitySource.getExplicitForeignKeyName() ),
entityBinding.getPrimaryTable(),
sourceColumns,
determineForeignKeyTargetTable( superEntityBinding, targetColumns ),
targetColumns
);
@ -2629,6 +2635,7 @@ public class Binder {
}
valueBindings.add(
new RelationalValueBinding(
defaultTable,
column,
valueSourceContainer.areValuesIncludedInInsertByDefault(),
valueSourceContainer.areValuesIncludedInUpdateByDefault() && !isImmutableNaturalId
@ -2670,6 +2677,7 @@ public class Binder {
);
valueBindings.add(
new RelationalValueBinding(
table,
column,
isIncludedInInsert,
!isImmutableNaturalId && isIncludedInUpdate
@ -2679,7 +2687,7 @@ public class Binder {
else {
final DerivedValue derivedValue =
table.locateOrCreateDerivedValue( ( (DerivedValueSource) valueSource ).getExpression() );
valueBindings.add( new RelationalValueBinding( derivedValue ) );
valueBindings.add( new RelationalValueBinding( table, derivedValue ) );
}
}
}
@ -2909,6 +2917,43 @@ public class Binder {
}
}
private TableSpecification determineForeignKeyTargetTable(final EntityBinding entityBinding,
final List<Column> targetColumns) {
TableSpecification table = null;
TableSpecification actualTable = null;
boolean first = true;
for ( Column column : targetColumns) {
if ( first ) {
table = column.getTable();
if ( entityBinding.hasTable( table.getLogicalName().getText() ) ) {
actualTable = table;
}
else {
if ( entityBinding.getHierarchyDetails().getInheritanceType() == InheritanceType.TABLE_PER_CLASS &&
column == entityBinding.getPrimaryTable().locateColumn( column.getColumnName().getText() ) ) {
actualTable = entityBinding.getPrimaryTable();
}
else {
throw bindingContext().makeMappingException( "improve this message!!!" );
}
}
first = false;
}
else {
if ( !table.equals( column.getTable() ) ) {
throw bindingContext().makeMappingException(
String.format(
"Join columns come from more than one table: %s, %s ",
table,
column.getTable()
)
);
}
}
}
return actualTable;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ simple instance helper methods
private void mapSourcesByName(final EntitySource rootEntitySource) {
String entityName = rootEntitySource.getEntityName();

View File

@ -50,14 +50,16 @@ public class ForeignKeyHelper {
public ForeignKey locateOrCreateForeignKey(
final String foreignKeyName,
final TableSpecification sourceTable,
final List<Column> sourceColumns,
final TableSpecification targetTable,
final List<Column> targetColumns) {
ForeignKey foreignKey = null;
if ( foreignKeyName != null ) {
foreignKey = locateAndBindForeignKeyByName( foreignKeyName, sourceColumns, targetColumns );
foreignKey = locateAndBindForeignKeyByName( foreignKeyName, sourceTable, sourceColumns, targetTable, targetColumns );
}
if ( foreignKey == null ) {
foreignKey = locateForeignKeyByColumnMapping( sourceColumns, targetColumns );
foreignKey = locateForeignKeyByColumnMapping( sourceTable, sourceColumns, targetTable, targetColumns );
if ( foreignKey != null && foreignKeyName != null ) {
if ( foreignKey.getName() == null ) {
// the foreign key name has not be initialized; set it to foreignKeyName
@ -78,19 +80,17 @@ public class ForeignKeyHelper {
}
if ( foreignKey == null ) {
// no foreign key found; create one
final TableSpecification sourceTable = sourceColumns.get( 0 ).getTable();
final TableSpecification targetTable = targetColumns.get( 0 ).getTable();
foreignKey = sourceTable.createForeignKey( targetTable, foreignKeyName );
bindForeignKeyColumns( foreignKey, sourceColumns, targetColumns );
bindForeignKeyColumns( foreignKey, sourceTable, sourceColumns, targetTable, targetColumns );
}
return foreignKey;
}
private static ForeignKey locateForeignKeyByColumnMapping(
final TableSpecification sourceTable,
final List<Column> sourceColumns,
final TableSpecification targetTable,
final List<Column> targetColumns) {
final TableSpecification sourceTable = sourceColumns.get( 0 ).getTable();
final TableSpecification targetTable = targetColumns.get( 0 ).getTable();
// check for an existing foreign key with the same source/target columns
ForeignKey foreignKey = null;
Iterable<ForeignKey> possibleForeignKeys = sourceTable.locateForeignKey( targetTable );
@ -109,15 +109,17 @@ public class ForeignKeyHelper {
private void bindForeignKeyColumns(
final ForeignKey foreignKey,
final TableSpecification sourceTable,
final List<Column> sourceColumns,
final TableSpecification targetTable,
final List<Column> targetColumns) {
if ( sourceColumns.size() != targetColumns.size() ) {
throw binder.bindingContext().makeMappingException(
String.format(
"Non-matching number columns in foreign key source columns [%s : %s] and target columns [%s : %s]",
sourceColumns.get( 0 ).getTable().getLogicalName().getText(),
sourceTable.getLogicalName().getText(),
sourceColumns.size(),
targetColumns.get( 0 ).getTable().getLogicalName().getText(),
targetTable.getLogicalName().getText(),
targetColumns.size()
)
);
@ -129,13 +131,13 @@ public class ForeignKeyHelper {
private ForeignKey locateAndBindForeignKeyByName(
final String foreignKeyName,
final TableSpecification sourceTable,
final List<Column> sourceColumns,
final TableSpecification targetTable,
final List<Column> targetColumns) {
if ( foreignKeyName == null ) {
throw new AssertionFailure( "foreignKeyName must be non-null." );
}
final TableSpecification sourceTable = sourceColumns.get( 0 ).getTable();
final TableSpecification targetTable = targetColumns.get( 0 ).getTable();
ForeignKey foreignKey = sourceTable.locateForeignKey( foreignKeyName );
if ( foreignKey != null ) {
if ( !targetTable.equals( foreignKey.getTargetTable() ) ) {
@ -151,7 +153,7 @@ public class ForeignKeyHelper {
// check if source and target columns have been bound already
if ( foreignKey.getColumnSpan() == 0 ) {
// foreign key was found, but no columns bound to it yet
bindForeignKeyColumns( foreignKey, sourceColumns, targetColumns );
bindForeignKeyColumns( foreignKey, sourceTable, sourceColumns, targetTable, targetColumns );
}
else {
// The located foreign key already has columns bound;

View File

@ -33,8 +33,10 @@ import org.hibernate.metamodel.internal.Binder;
import org.hibernate.metamodel.spi.binding.AttributeBinding;
import org.hibernate.metamodel.spi.binding.CompositeAttributeBinding;
import org.hibernate.metamodel.spi.binding.SingularAttributeBinding;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.Value;
import org.hibernate.metamodel.spi.source.ExplicitHibernateTypeSource;
import org.hibernate.metamodel.spi.source.ForeignKeyContributingSource;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.metamodel.spi.source.ToOneAttributeSource;
import org.hibernate.type.ForeignKeyDirection;

View File

@ -28,8 +28,10 @@ import java.util.List;
import org.hibernate.jaxb.spi.hbm.JaxbColumnElement;
import org.hibernate.jaxb.spi.hbm.JaxbKeyElement;
import org.hibernate.metamodel.spi.relational.ForeignKey;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.Value;
import org.hibernate.metamodel.spi.source.AttributeSourceContainer;
import org.hibernate.metamodel.spi.source.ForeignKeyContributingSource;
import org.hibernate.metamodel.spi.source.PluralAttributeKeySource;
import org.hibernate.metamodel.spi.source.RelationalValueSource;

View File

@ -31,8 +31,10 @@ import org.hibernate.jaxb.spi.hbm.JaxbColumnElement;
import org.hibernate.jaxb.spi.hbm.JaxbFetchStyleAttribute;
import org.hibernate.jaxb.spi.hbm.JaxbJoinElement;
import org.hibernate.jaxb.spi.hbm.JaxbOnDeleteAttribute;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.Value;
import org.hibernate.metamodel.spi.source.ColumnSource;
import org.hibernate.metamodel.spi.source.ForeignKeyContributingSource;
import org.hibernate.metamodel.spi.source.InLineViewSource;
import org.hibernate.metamodel.spi.source.RelationalValueSource;
import org.hibernate.metamodel.spi.source.SecondaryTableSource;

View File

@ -70,9 +70,9 @@ public class BackRefAttributeBinding extends BasicAttributeBinding {
}
else {
relationalValueBindings = new ArrayList<RelationalValueBinding>( );
for ( RelationalValueBinding keyRelationalValueBindings : pluralAttributeBinding.getPluralAttributeKeyBinding().getRelationalValueBindings() ) {
Column keyColumn = (Column) keyRelationalValueBindings.getValue();
relationalValueBindings.add( new RelationalValueBinding( keyColumn, true, false ) );
for ( RelationalValueBinding keyRelationalValueBinding : pluralAttributeBinding.getPluralAttributeKeyBinding().getRelationalValueBindings() ) {
Column keyColumn = (Column) keyRelationalValueBinding.getValue();
relationalValueBindings.add( new RelationalValueBinding( keyRelationalValueBinding.getTable(), keyColumn, true, false ) );
}
}
return relationalValueBindings;

View File

@ -184,6 +184,11 @@ public class EntityBinding extends AbstractAttributeBindingContainer implements
this.primaryTable = primaryTable;
}
public boolean hasTable(String tableName) {
return tableName.equals( getPrimaryTableName() ) ||
secondaryTables.containsKey( Identifier.toIdentifier( tableName ) );
}
public TableSpecification locateTable(String tableName) {
if ( tableName == null || tableName.equals( getPrimaryTableName() ) ) {
return primaryTable;
@ -809,8 +814,7 @@ public class EntityBinding extends AbstractAttributeBindingContainer implements
return 0;
}
int result=1;
Value value = attributeBinding.getRelationalValueBindings().get( 0 ).getValue();
TableSpecification table = value.getTable();
TableSpecification table = attributeBinding.getRelationalValueBindings().get( 0 ).getTable();
for ( SecondaryTable secondaryTable : getEntitiesSecondaryTableClosure() ) {
if ( secondaryTable.getSecondaryTableReference() == table ) {
return result;

View File

@ -274,7 +274,7 @@ public class EntityIdentifier {
// the first one to get the TableSpecification.
final RelationalValueBinding relationalValueBinding = relationalValueBindings.get( 0 );
final TableSpecification table = relationalValueBinding.getValue().getTable();
final TableSpecification table = relationalValueBinding.getTable();
if ( !Column.class.isInstance( relationalValueBinding.getValue() ) ) {
throw new MappingException(
"Cannot create an IdentifierGenerator because the value is not a column: " +

View File

@ -71,7 +71,7 @@ public class PluralAttributeKeyBinding {
public TableSpecification getCollectionTable() {
// TODO: get table directly from relationalValueBindingContainer
return relationalValueBindingContainer.relationalValueBindings().get( 0 ).getValue().getTable();
return relationalValueBindingContainer.relationalValueBindings().get( 0 ).getTable();
}
/**

View File

@ -25,6 +25,7 @@ package org.hibernate.metamodel.spi.binding;
import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.DerivedValue;
import org.hibernate.metamodel.spi.relational.TableSpecification;
import org.hibernate.metamodel.spi.relational.Value;
/**
@ -37,25 +38,32 @@ import org.hibernate.metamodel.spi.relational.Value;
* @author Steve Ebersole
*/
public class RelationalValueBinding {
private final TableSpecification table;
private final Value value;
private final boolean includeInInsert;
private final boolean includeInUpdate;
private final boolean isDerived;
public RelationalValueBinding(final DerivedValue value) {
public RelationalValueBinding(final TableSpecification table, final DerivedValue value) {
this.table = table;
this.value = value;
this.includeInInsert = false;
this.includeInUpdate = false;
this.isDerived = true;
}
public RelationalValueBinding(final Column value, final boolean includeInInsert, final boolean includeInUpdate) {
public RelationalValueBinding(final TableSpecification table, final Column value, final boolean includeInInsert, final boolean includeInUpdate) {
this.table = table;
this.value = value;
this.includeInInsert = includeInInsert;
this.includeInUpdate = includeInUpdate;
this.isDerived = false;
}
public TableSpecification getTable() {
return table;
}
/**
* Retrieve the relational value bound here.
*

View File

@ -81,10 +81,6 @@ public abstract class AbstractTableSpecification implements TableSpecification {
@Override
public boolean hasValue(Value value) {
//shortcut
if ( this != value.getTable() ) {
return false;
}
return valueMap.containsValue( value );
}

View File

@ -39,6 +39,7 @@ public class DenormalizedTable extends Table {
public DenormalizedTable(Schema database, Identifier logicalName, Identifier physicalName, Table includedTable) {
super( database, logicalName, physicalName );
this.includedTable = includedTable;
this.includedTable.setHasDenormalizedTables( true );
}
@Override

View File

@ -127,15 +127,15 @@ public class ForeignKey extends AbstractConstraint {
}
private void checkTargetTable(Column targetColumn) {
if ( targetColumn.getTable() != getTargetTable() ) {
throw new AssertionFailure(
String.format(
"Unable to add column to constraint; tables [%s, %s] did not match",
targetColumn.getTable().toLoggableString(),
getTargetTable().toLoggableString()
)
);
}
// if ( targetColumn.getTable() != getTargetTable() ) {
// throw new AssertionFailure(
// String.format(
// "Unable to add column to constraint; tables [%s, %s] did not match",
// targetColumn.getTable().toLoggableString(),
// getTargetTable().toLoggableString()
// )
// );
// }
}
@Override

View File

@ -48,6 +48,7 @@ public class Table extends AbstractTableSpecification implements Exportable {
private ObjectName qualifiedName;
private String exportIdentifier;
private boolean isPhysicalTable = true;
private boolean hasDenormalizedTables = false;
private final Set<Index> indexes = new LinkedHashSet<Index>();
private final Set<UniqueKey> uniqueKeys = new LinkedHashSet<UniqueKey>();
@ -108,6 +109,14 @@ public class Table extends AbstractTableSpecification implements Exportable {
isPhysicalTable = physicalTable;
}
public boolean hasDenormalizedTables() {
return hasDenormalizedTables;
}
protected void setHasDenormalizedTables(boolean hasDenormalizedTables) {
this.hasDenormalizedTables = hasDenormalizedTables;
}
/**
* Gets the qualified table name.
*

View File

@ -982,7 +982,7 @@ public abstract class AbstractEntityPersister
for ( RelationalValueBinding valueBinding : singularAttributeBinding.getRelationalValueBindings() ) {
colAliases[k] = valueBinding.getValue().getAlias(
factory.getDialect(),
valueBinding.getValue().getTable()
valueBinding.getTable()
);
if ( valueBinding.isDerived() ) {
foundFormula = true;
@ -1106,7 +1106,7 @@ public abstract class AbstractEntityPersister
aliases.add(
col.getAlias(
factory.getDialect(),
col.getTable()
valueBinding.getTable()
)
);
columnsLazy.add( lazy );
@ -2319,7 +2319,7 @@ public abstract class AbstractEntityPersister
for ( RelationalValueBinding relationalValueBinding : singularProp.getRelationalValueBindings() ) {
aliases[l] = relationalValueBinding.getValue().getAlias(
getFactory().getDialect(),
relationalValueBinding.getValue().getTable()
relationalValueBinding.getTable()
);
if ( relationalValueBinding.isDerived() ) {
cols[l] = ( (DerivedValue) relationalValueBinding.getValue() ).getExpression(); // TODO: skip formulas?

View File

@ -115,10 +115,18 @@ public class SchemaCreatorImpl implements SchemaCreator {
for ( Table table : schema.getTables() ) {
for ( ForeignKey foreignKey : table.getForeignKeys() ) {
// only add the foreign key if its target is a physical table
if ( Table.class.isInstance( foreignKey.getTargetTable() ) ) {
checkExportIdentifier( foreignKey, exportIdentifiers );
applySqlStrings( targets, dialect.getForeignKeyExporter().getSqlCreateStrings( foreignKey, jdbcEnvironment ) );
// only add the foreign key if its source and target are both physical tables
// and if the target table does not have any denormalized tables.
if ( Table.class.isInstance( foreignKey.getTable() ) &&
Table.class.isInstance( foreignKey.getTargetTable() ) ) {
Table sourceTable = Table.class.cast( foreignKey.getTable() );
Table targetTable = Table.class.cast( foreignKey.getTargetTable() );
if ( sourceTable.isPhysicalTable() &&
targetTable.isPhysicalTable() &&
!targetTable.hasDenormalizedTables() ) {
checkExportIdentifier( foreignKey, exportIdentifiers );
applySqlStrings( targets, dialect.getForeignKeyExporter().getSqlCreateStrings( foreignKey, jdbcEnvironment ) );
}
}
}
}

View File

@ -87,13 +87,21 @@ public class SchemaDropperImpl implements SchemaDropper {
// we need to drop constraints prior to dropping table
for ( ForeignKey foreignKey : table.getForeignKeys() ) {
// only add the foreign key if its target is a physical table
if ( Table.class.isInstance( foreignKey.getTargetTable() ) ) {
checkExportIdentifier( foreignKey, exportIdentifiers );
applySqlStrings(
targets,
dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, jdbcEnvironment )
);
// only add the foreign key if its source and target are both physical tables
// and if the target table does not have any denormalized tables.
if ( Table.class.isInstance( foreignKey.getTable() ) &&
Table.class.isInstance( foreignKey.getTargetTable() ) ) {
Table sourceTable = Table.class.cast( foreignKey.getTable() );
Table targetTable = Table.class.cast( foreignKey.getTargetTable() );
if ( sourceTable.isPhysicalTable() &&
targetTable.isPhysicalTable() &&
!targetTable.hasDenormalizedTables() ) {
checkExportIdentifier( foreignKey, exportIdentifiers );
applySqlStrings(
targets,
dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, jdbcEnvironment )
);
}
}
}

View File

@ -88,12 +88,12 @@ public class SchemaValidatorImpl implements SchemaValidator {
);
}
validateColumnType( column, columnInformation );
validateColumnType( table, column, columnInformation );
}
}
}
protected void validateColumnType(Column column, ColumnInformation columnInformation) {
protected void validateColumnType(Table table, Column column, ColumnInformation columnInformation) {
// this is the old Hibernate check...
final boolean typesMatch = column.getJdbcDataType().getTypeCode() == columnInformation.getTypeCode()
|| column.getSqlType().toLowerCase().startsWith( columnInformation.getTypeName().toLowerCase() );
@ -102,7 +102,7 @@ public class SchemaValidatorImpl implements SchemaValidator {
String.format(
"Schema-validation: wrong column type encountered in column [%s] in table [%s]; found [%s], but expecting [%s]",
column.getColumnName().getText(),
((Table) column.getTable()).getTableName().toText(),
table.getTableName().toText(),
columnInformation.getTypeName().toLowerCase(),
column.getSqlType().toLowerCase()
)

View File

@ -88,7 +88,7 @@ public class SecondaryTableTest extends BaseAnnotationBindingTestCase {
assertEquals( 1, nameAttrBinding.getRelationalValueBindings().size() );
RelationalValueBinding valueBinding = nameAttrBinding.getRelationalValueBindings().get( 0 );
assertFalse( valueBinding.isDerived() );
assertSame( table, valueBinding.getValue().getTable() );
assertSame( table, valueBinding.getTable() );
assertSame( column, valueBinding.getValue() );
}

View File

@ -216,7 +216,7 @@ public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {
// locate the foreignKey
boolean sourceColumnFound = false;
for ( ForeignKey fk : column.getTable().getForeignKeys() ) {
for ( ForeignKey fk : relationalValueBinding.getTable().getForeignKeys() ) {
for ( Column sourceColumn : fk.getSourceColumns() ) {
if ( sourceColumn == column ) {
assertFalse( "source column not found in more than one foreign key", sourceColumnFound );

View File

@ -74,6 +74,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
List<RelationalValueBinding> valueBindings = new ArrayList<RelationalValueBinding>();
valueBindings.add(
new RelationalValueBinding(
table,
idColumn,
true,
true

View File

@ -179,7 +179,7 @@ public abstract class AbstractBasicCollectionBindingTests extends BaseUnitTestCa
List<RelationalValueBinding> keyRelationalValueBindings = keyBinding.getRelationalValueBindings();
assertNotNull( keyRelationalValueBindings );
for( RelationalValueBinding keyRelationalValueBinding : keyRelationalValueBindings ) {
assertSame( collectionTable, keyRelationalValueBinding.getValue().getTable() );
assertSame( collectionTable, keyRelationalValueBinding.getTable() );
}
assertEquals( 1, keyRelationalValueBindings.size() );
assertEquals( 1, expectedKeyTargetAttributeBinding.getRelationalValueBindings().size() );

View File

@ -72,42 +72,42 @@ public class GeneratedNamesForDifferentConstraintTypes {
// Add primary key columns to PK constraints and add generated name
assertTrue( generatedNames.add( addColumnToPrimaryKey( pkColTab1 ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( pkColTab2 ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( pkColTab1Ref ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( pkColTab2Ref ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( tab1, pkColTab1 ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( tab2, pkColTab2 ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( refTab1, pkColTab1Ref ).generateName() ) );
assertTrue( generatedNames.add( addColumnToPrimaryKey( refTab2, pkColTab2Ref ).generateName() ) );
// add constraints to tab1
// add other types of constraints to the PK column for tab1
assertTrue( generatedNames.add( createUniqueKey( pkColTab1 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( pkColTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( pkColTab1, tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( pkColTab1, tab1, col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createUniqueKey( tab1, pkColTab1 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( tab1, pkColTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, pkColTab1, tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, pkColTab1, tab1, col1Tab1 ).generateName() ) );
// add constraints to 1st non-PK column for tab1 (col1Tab1)
assertTrue( generatedNames.add( createUniqueKey( col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, tab1, col2Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab1, refTab2, col2Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createUniqueKey( tab1, col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( tab1, col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, tab1, col2Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col1Tab1, refTab2, col2Tab2Ref ).generateName() ) );
// add constraints to 2nd non-PK column for tab1 (col2Tab1)
assertTrue( generatedNames.add( createUniqueKey( col2Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( col2Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, tab1, col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab1, refTab2, col2Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createUniqueKey( tab1, col2Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( tab1, col2Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, tab1, col1Tab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab1, col2Tab1, refTab2, col2Tab2Ref ).generateName() ) );
// add multi-column constraints to tab1
List<Column> colsTab1 = new ArrayList<Column>( );
@ -139,34 +139,34 @@ public class GeneratedNamesForDifferentConstraintTypes {
// add constraints to tab2
// add other types of constraints to the PK column for tab2
assertTrue( generatedNames.add( createUniqueKey( pkColTab2 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( pkColTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( pkColTab2, tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( pkColTab2, tab2, col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createUniqueKey( tab2, pkColTab2 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( tab2, pkColTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, pkColTab2, tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, pkColTab2, tab2, col1Tab2 ).generateName() ) );
// add constraints to 1st non-PK column for tab2 (col1Tab2)
assertTrue( generatedNames.add( createUniqueKey( col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, tab2, col2Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col1Tab2, refTab2, col2Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createUniqueKey( tab2, col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( tab2, col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, tab2, col2Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col1Tab2, refTab2, col2Tab2Ref ).generateName() ) );
// add constraints to 2nd non-PK column (col2Tab1) for tab1
assertTrue( generatedNames.add( createUniqueKey( col2Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( col2Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, tab2, col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( col2Tab2, refTab2, col2Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createUniqueKey( tab2, col2Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createIndex( tab2, col2Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, tab2, col1Tab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, refTab1 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, refTab1, col1Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, refTab1, col2Tab1Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, refTab2 ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, refTab2, col1Tab2Ref ).generateName() ) );
assertTrue( generatedNames.add( createForeignKey( tab2, col2Tab2, refTab2, col2Tab2Ref ).generateName() ) );
}
@Test
@ -179,8 +179,9 @@ public class GeneratedNamesForDifferentConstraintTypes {
Column referencedPKColumn = referencedTable.locateColumn( COLUMN_NAMES[0] );
referencedTable.getPrimaryKey().addColumn( referencedPKColumn );
ForeignKey fkImplicitFKMapping = createForeignKey( fkColumn, referencedTable );
ForeignKey fkImplicitFKMapping = createForeignKey( table, fkColumn, referencedTable );
ForeignKey fkExplicitFKMappingToPK = createForeignKey(
table,
fkColumn,
referencedTable,
referencedPKColumn
@ -188,20 +189,20 @@ public class GeneratedNamesForDifferentConstraintTypes {
assertEquals( fkImplicitFKMapping.generateName(), fkExplicitFKMappingToPK.generateName() );
}
private PrimaryKey addColumnToPrimaryKey(Column column) {
PrimaryKey primaryKey = column.getTable().getPrimaryKey();
private PrimaryKey addColumnToPrimaryKey(TableSpecification tableSpecification, Column column) {
PrimaryKey primaryKey = tableSpecification.getPrimaryKey();
primaryKey.addColumn( column );
return primaryKey;
}
private ForeignKey createForeignKey(Column column, TableSpecification referencedTable ) {
ForeignKey foreignKey = column.getTable().createForeignKey( referencedTable, null );
private ForeignKey createForeignKey(TableSpecification sourceTable, Column column, TableSpecification referencedTable ) {
ForeignKey foreignKey = sourceTable.createForeignKey( referencedTable, null );
foreignKey.addColumn( column );
return foreignKey;
}
private ForeignKey createForeignKey(Column column, TableSpecification referencedTable, Column referencedColumn ) {
ForeignKey foreignKey = column.getTable().createForeignKey( referencedTable, null );
private ForeignKey createForeignKey(TableSpecification sourceTable, Column column, TableSpecification referencedTable, Column referencedColumn ) {
ForeignKey foreignKey = sourceTable.createForeignKey( referencedTable, null );
foreignKey.addColumnMapping( column, referencedColumn );
return foreignKey;
}
@ -218,8 +219,8 @@ public class GeneratedNamesForDifferentConstraintTypes {
return foreignKey;
}
private UniqueKey createUniqueKey(Column column) {
UniqueKey uniqueKey = column.getTable().getOrCreateUniqueKey( null );
private UniqueKey createUniqueKey(TableSpecification table, Column column) {
UniqueKey uniqueKey = table.getOrCreateUniqueKey( null );
uniqueKey.addColumn( column );
return uniqueKey;
}
@ -232,8 +233,8 @@ public class GeneratedNamesForDifferentConstraintTypes {
return uniqueKey;
}
private Index createIndex(Column column) {
Index index = column.getTable().getOrCreateIndex( null );
private Index createIndex(TableSpecification table, Column column) {
Index index = table.getOrCreateIndex( null );
index.addColumn( column );
return index;
}

View File

@ -105,9 +105,7 @@ public class NamingStrategyTest extends BaseCoreFunctionalTestCase {
assertEquals( 1, singularAttributeBinding.getRelationalValueBindings().size() );
RelationalValueBinding valueBinding = singularAttributeBinding.getRelationalValueBindings().get( 0 );
assertFalse( valueBinding.isDerived() );
org.hibernate.metamodel.spi.relational.Column column = (org.hibernate.metamodel.spi.relational.Column) valueBinding
.getValue();
Table table = (Table)column.getTable();
Table table = (Table)valueBinding.getTable();
assertEquals( "TAB_ITEMS_SEC", table.getPhysicalName().getText() );
@ -115,9 +113,7 @@ public class NamingStrategyTest extends BaseCoreFunctionalTestCase {
assertEquals( 1, singularAttributeBinding.getRelationalValueBindings().size() );
valueBinding = singularAttributeBinding.getRelationalValueBindings().get( 0 );
assertFalse( valueBinding.isDerived() );
column = (org.hibernate.metamodel.spi.relational.Column) valueBinding
.getValue();
table = (Table)column.getTable();
table = (Table)valueBinding.getTable();
assertEquals( "TAB_ITEMS", table.getPhysicalName().getText());
}

View File

@ -38,7 +38,6 @@ import static org.junit.Assert.assertTrue;
/**
* @author Gavin King
*/
@FailureExpectedWithNewMetamodel
public class UnionSubclassPropertyRefTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {

View File

@ -39,7 +39,6 @@ import static org.junit.Assert.assertNotNull;
* @author Strong Liu <stliu@redhat.com>
*/
@TestForIssue( jiraKey = "HHH-4825" )
@FailureExpectedWithNewMetamodel
public class SellCarTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {

View File

@ -53,7 +53,6 @@ public class UnionSubclassTest extends BaseCoreFunctionalTestCase {
}
@Test
@FailureExpectedWithNewMetamodel
public void testUnionSubclass() {
Session s = openSession();
Transaction t = s.beginTransaction();