Search in sources :

Example 1 with ForeignKeyInformationImpl

use of org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl in project hibernate-orm by hibernate.

the class CheckForExistingForeignKeyTest method getForeignKeyInformation.

/**
 * @param referencedTableName - String
 * @param referencingColumnName - String
 * @param keyName - String
 * @return ForeignKeyInformation
 */
private ForeignKeyInformation getForeignKeyInformation(String referencedTableName, String referencingColumnName, String keyName) {
    List<ColumnReferenceMapping> columnMappingList = new ArrayList<>();
    ColumnInformation referencingColumnMetadata = getColumnInformation("-", referencingColumnName);
    ColumnInformation referencedColumnMetadata = getColumnInformation(referencedTableName, "-");
    ColumnReferenceMapping columnReferenceMapping = new ColumnReferenceMappingImpl(referencingColumnMetadata, referencedColumnMetadata);
    columnMappingList.add(columnReferenceMapping);
    ForeignKeyInformationImpl foreignKeyInformation = new ForeignKeyInformationImpl(new Identifier(keyName, false), columnMappingList);
    return foreignKeyInformation;
}
Also used : Identifier(org.hibernate.boot.model.naming.Identifier) ForeignKeyInformationImpl(org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl) ColumnInformation(org.hibernate.tool.schema.extract.spi.ColumnInformation) ArrayList(java.util.ArrayList) ColumnReferenceMapping(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation.ColumnReferenceMapping)

Example 2 with ForeignKeyInformationImpl

use of org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl in project hibernate-orm by hibernate.

the class CheckForExistingForeignKeyTest method testKeyWithSameNameNotExists.

/**
 * Check detection of existing foreign keys with the same name exists.
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testKeyWithSameNameNotExists() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    foreignKey.setName("objectId2id_1");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(new ForeignKeyInformationImpl(new Identifier("objectId2id_2", false), new ArrayList<>()));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    // foreignKey name with same name should match
    boolean found = (boolean) method.invoke(new SchemaMigrator(), foreignKey, tableInformation);
    Assert.assertFalse("Key should not be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) ForeignKeyInformationImpl(org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) Test(org.junit.Test)

Example 3 with ForeignKeyInformationImpl

use of org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl in project hibernate-orm by hibernate.

the class CheckForExistingForeignKeyTest method testKeyWithSameNameExists.

/**
 * Check detection of existing foreign keys with the same name exists.
 *
 * @throws SecurityException - error
 * @throws NoSuchMethodException - error
 * @throws InvocationTargetException - error
 * @throws IllegalArgumentException - error
 * @throws IllegalAccessException - error
 * @throws NoSuchFieldException - error
 */
@Test
public void testKeyWithSameNameExists() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    // Get the private method
    Method method = AbstractSchemaMigrator.class.getDeclaredMethod("checkForExistingForeignKey", ForeignKey.class, TableInformation.class);
    method.setAccessible(true);
    ForeignKey foreignKey = new ForeignKey();
    foreignKey.setName("objectId2id");
    foreignKey.addColumn(new Column("id"));
    foreignKey.setReferencedTable(new Table("table2"));
    InformationExtractor informationExtractor = Mockito.mock(InformationExtractor.class);
    IdentifierHelper identifierHelper = new IdentifierHelperImpl();
    List<ForeignKeyInformation> fks = new ArrayList<>();
    fks.add(new ForeignKeyInformationImpl(new Identifier("objectId2id", false), new ArrayList<>()));
    Mockito.when(informationExtractor.getForeignKeys(Mockito.any())).thenReturn(fks);
    Name schemaName = new Name(new Identifier("-", false), new Identifier("-", false));
    QualifiedTableName tableName = new QualifiedTableName(schemaName, new Identifier("-", false));
    TableInformation tableInformation = new TableInformationImpl(informationExtractor, identifierHelper, tableName, false, null);
    // foreignKey name with same name should match
    boolean found = (boolean) method.invoke(new SchemaMigrator(), foreignKey, tableInformation);
    Assert.assertTrue("Key should be found", found);
}
Also used : QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) Table(org.hibernate.mapping.Table) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ForeignKey(org.hibernate.mapping.ForeignKey) IdentifierHelper(org.hibernate.engine.jdbc.env.spi.IdentifierHelper) Name(org.hibernate.boot.model.relational.Namespace.Name) QualifiedTableName(org.hibernate.boot.model.relational.QualifiedTableName) ForeignKeyInformation(org.hibernate.tool.schema.extract.spi.ForeignKeyInformation) Identifier(org.hibernate.boot.model.naming.Identifier) TableInformationImpl(org.hibernate.tool.schema.extract.internal.TableInformationImpl) AbstractSchemaMigrator(org.hibernate.tool.schema.internal.AbstractSchemaMigrator) Column(org.hibernate.mapping.Column) ForeignKeyInformationImpl(org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl) InformationExtractor(org.hibernate.tool.schema.extract.spi.InformationExtractor) TableInformation(org.hibernate.tool.schema.extract.spi.TableInformation) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 Identifier (org.hibernate.boot.model.naming.Identifier)3 ForeignKeyInformationImpl (org.hibernate.tool.schema.extract.internal.ForeignKeyInformationImpl)3 Method (java.lang.reflect.Method)2 Name (org.hibernate.boot.model.relational.Namespace.Name)2 QualifiedTableName (org.hibernate.boot.model.relational.QualifiedTableName)2 IdentifierHelper (org.hibernate.engine.jdbc.env.spi.IdentifierHelper)2 Column (org.hibernate.mapping.Column)2 ForeignKey (org.hibernate.mapping.ForeignKey)2 Table (org.hibernate.mapping.Table)2 TableInformationImpl (org.hibernate.tool.schema.extract.internal.TableInformationImpl)2 ForeignKeyInformation (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation)2 InformationExtractor (org.hibernate.tool.schema.extract.spi.InformationExtractor)2 TableInformation (org.hibernate.tool.schema.extract.spi.TableInformation)2 AbstractSchemaMigrator (org.hibernate.tool.schema.internal.AbstractSchemaMigrator)2 Test (org.junit.Test)2 ColumnInformation (org.hibernate.tool.schema.extract.spi.ColumnInformation)1 ColumnReferenceMapping (org.hibernate.tool.schema.extract.spi.ForeignKeyInformation.ColumnReferenceMapping)1