persistence@glassfish.java.net

Question about ForeignKeyConstraints

From: Wonseok Kim <guruwons_at_gmail.com>
Date: Thu, 26 Oct 2006 18:53:14 +0900

Hi Tom,

I'm trying to fix issue 1072 - ddl generation of composite foreign key
constraints.
I think the ForeignKeyConstraints class is the right one to use if composite
foreign key, but there is another foreign key setting in FieldDefinition.

public class FieldDefinition implements Serializable, Cloneable {
...
    protected String foreignKeyFieldName;//fully-qualified foreign key field
name

But current code shows that if above foreignKeyFieldName is set, it ignores
the TableDefinition.foreignKeys (ForeignKeyConstraints). See below.

TableDefinition:
    private void buildForeignFieldTypes(final AbstractSession session) {
        Hashtable fieldTypes = session.getPlatform().getClassTypes();
        FieldDefinition field = null;

        Vector foreignKeysClone = (Vector)getForeignKeys().clone();

        setForeignKeys(new Vector());
        for (Enumeration enumtr = getFields().elements();
enumtr.hasMoreElements();) {
            field = (FieldDefinition)enumtr.nextElement();
            if (field.getForeignKeyFieldName() != null) {
                addForeignKeyConstraint(buildForeignKeyConstraint(field,
this, session.getPlatform()));

            }
            if (field.getType() == null) {
                field.setType((Class)fieldTypes.get(field.getTypeName()));
                if (field.getType() != null) {
                    field.setTypeName(null);
                }
            }
        }

        /* bug #2997188 constraints not getting generated when
creating/replacing tables via TableCreator
           had to add the next few lines instead of removing the above code
for backwards compatibility */
        if (getForeignKeys().isEmpty()) {
            //if foreignKeys is empty then we know we are not in 2.5
            setForeignKeys(foreignKeysClone);
        }
    }

I'm curious, should we use either foreignKeyFieldName or foreignKeys
(ForeignKeyConstraints)? We could not mix both of them? Is there any reason
for not allowing this?
entity-persistence-tests table creators are using only foreignKeyFieldName
now, but If I would like to add composite foreign key constraints, how can I
mix them?

Also, I don't know there is any policy about code, but could I use Java SE 5
Generics in schema generation codes? This makes us avoid type-mismatch
mistakes and easy to read code.

Regards,
- Wonseok