works. Here's the first-draft modified Table method. I may still need to add similar support for check constraints in the CREATE TABLE statement, and clean everything up, but I am successfully generating a working database complete with foreign keys using hbm2dll. :)
Since I chose to use the Dialect.hasAlterTable() method, there is no need to change Configuration to suppress the ALTER TABLE statements.
Code:
public String sqlCreateString(Dialect dialect, Mapping p, String defaultCatalog, String defaultSchema) throws HibernateException {
StringBuffer buf = new StringBuffer("create table ").append(getQualifiedName(dialect, defaultCatalog, defaultSchema)).append(" (");
boolean identityColumn = idValue != null && idValue.createIdentifierGenerator(dialect, defaultCatalog, defaultSchema, null) instanceof IdentityGenerator;
// Try to find out the name of the primary key to create it as identity if the IdentityGenerator is used
String pkname = null;
if (primaryKey != null && identityColumn) {
pkname = ((Column) primaryKey.getColumnIterator().next()).getQuotedName(dialect);
}
Iterator iter = getColumnIterator();
while (iter.hasNext()) {
Column col = (Column) iter.next();
buf.append(col.getQuotedName(dialect)).append(' ');
if (identityColumn && col.getQuotedName(dialect).equals(pkname)) {
// to support dialects that have their own identity data type
if (dialect.hasDataTypeInIdentityColumn()) {
buf.append(col.getSqlType(dialect, p));
}
buf.append(' ').append(dialect.getIdentityColumnString(col.getSqlTypeCode(p)));
} else {
buf.append(col.getSqlType(dialect, p));
if (col.isNullable()) {
buf.append(dialect.getNullColumnString());
} else {
buf.append(" not null");
}
}
if (col.isUnique()) {
if (dialect.supportsUnique()) {
buf.append(" unique");
} else {
UniqueKey uk = getOrCreateUniqueKey(col.getQuotedName(dialect) + '_');
uk.addColumn(col);
}
}
if (col.hasCheckConstraint() && dialect.supportsColumnCheck()) {
buf.append(" check (").append(col.getCheckConstraint()).append(")");
}
if (iter.hasNext()) {
buf.append(", ");
}
}
if (primaryKey != null) {
/*if ( dialect instanceof HSQLDialect && identityColumn ) {
// skip the primary key definition
// ugly, ugly hack!
}
else {*/
buf.append(", ").append(primaryKey.sqlConstraintString(dialect));
//}
}
if (dialect.supportsUniqueConstraintInCreateAlterTable()) {
Iterator ukiter = getUniqueKeyIterator();
while (ukiter.hasNext()) {
UniqueKey uk = (UniqueKey) ukiter.next();
buf.append(", ").append(uk.sqlConstraintString(dialect));
}
}
// **** NEW CODE START ****
if (foreignKeys != null && !dialect.hasAlterTable()) {
System.out.println("iterate fks");
for (Iterator fkIter = foreignKeys.values().iterator(); fkIter.hasNext();) {
ForeignKey fk = (ForeignKey) fkIter.next();
if (fk.isPhysicalConstraint()) {
buf.append(", foreign key (");
for (Iterator fkColIter = fk.getColumnIterator(); fkColIter.hasNext();) {
final Column column = (Column) fkColIter.next();
buf.append(column.getName());
if (fkColIter.hasNext()) {
buf.append(", ");
}
}
}
buf.append(") references ");
buf.append(fk.getReferencedTable().getName() + " (");
Iterator fkRefIter = fk.getReferencedTable().getPrimaryKey().getColumnIterator();
while (fkRefIter.hasNext()){
final Column column = (Column) fkRefIter.next();
buf.append(column.getName());
if (fkRefIter.hasNext()) {
buf.append(", ");
}
}
buf.append(")");
}
}
// **** NEW CODE END ****
/*Iterator idxiter = getIndexIterator();
while ( idxiter.hasNext() ) {
Index idx = (Index) idxiter.next();
buf.append(',').append( idx.sqlConstraintString(dialect) );
}*/
if (dialect.supportsTableCheck()) {
Iterator chiter = checkConstraints.iterator();
while (chiter.hasNext()) {
buf.append(", check (").append(chiter.next()).append(')');
}
}
return buf.append(')').append(dialect.getTableTypeString()).toString();
}