persistence@glassfish.java.net

Re: Question about ForeignKeyConstraints

From: Wonseok Kim <guruwons_at_gmail.com>
Date: Mon, 30 Oct 2006 16:52:48 +0900

TopLink Team, please shed light on this so I can go ahead.
I added some comments in the below,

On 10/26/06, Wonseok Kim <guruwons_at_gmail.com> wrote:
>
> 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?


If there is no problem, I would like to support for both
ForeignKeyConstraint object and foreignKeyFieldName by removing
setForeignKeys(new Vector()) line and below lines.

        /* 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);
        }

Then we can use both FieldDefinition.foreignKeyFieldName (for simple foreign
key) and addForeignKeyConstraint (for composite foreign key).
What do you think?

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
>
>