Example usage for org.hibernate.mapping ForeignKey addColumn

List of usage examples for org.hibernate.mapping ForeignKey addColumn

Introduction

In this page you can find the example usage for org.hibernate.mapping ForeignKey addColumn.

Prototype

public void addColumn(Column column) 

Source Link

Usage

From source file:net.lshift.hibernate.migrations.AlterTableBuilder.java

License:Apache License

public AlterTableBuilder addForeignKey(String name, String[] columnNames, String referencedTable,
        String[] referencedColumns) {
    ForeignKey fk = new ForeignKey();
    fk.setName(name);/*w ww . j  av a 2 s .co m*/
    for (String col : columnNames)
        fk.addColumn(new Column(col));
    fk.setTable(new Table(table));

    PrimaryKey refPrimaryKey = new PrimaryKey();
    for (String col : referencedColumns)
        refPrimaryKey.addColumn(new Column(col));
    Table refTable = new Table(referencedTable);
    refTable.setPrimaryKey(refPrimaryKey);

    fk.setReferencedTable(refTable);

    String defaultCatalog = config.getProperties().getProperty(Environment.DEFAULT_CATALOG);
    String defaultSchema = config.getProperties().getProperty(Environment.DEFAULT_SCHEMA);

    // fk.sqlConstraintString appears to generate incorrect SQL against MySQL in some instances.
    // The referenced columns are not always correctly listed.
    //    alterFragments.add(" add index " + fk.getName() + " (" + StringHelper.join(", ", columnNames) +
    //        "), add constraint " + fk.getName() + " foreign key (" + StringHelper.join(", ", columnNames) +
    //        " references " + referencedTable + " (" + StringHelper.join(", ", referencedColumns) + ")");
    alterFragments.add(fk.sqlConstraintString(dialect, fk.getName(), defaultCatalog, defaultSchema));
    return this;
}