persistence@glassfish.java.net

Code review for Issue 1179 unique-constraint used in <table> element of mapping file throws exception on creation of the entity manager

From: Wonseok Kim <guruwons_at_gmail.com>
Date: Wed, 8 Nov 2006 21:35:18 +0900

Hi, Tom

Here is the another fix for DDL generation.
https://glassfish.dev.java.net/issues/show_bug.cgi?id=1179

Please review.

Situation:
1. @UniqueConstraint.columNames()(or its equivalent xml element) should be
mapped to one unique constraint. However, some codes had handled each column
of @UniqueConstraint.columnNames() as a separate unique constraint. This is
the first error.
Then, it calls DatabaseTable.addUniqueConstraint(String columnName) which
just add the columnName to underlying uniqueConstraints Vector. There is
also addUniqueConstraints(String[] columnNames) method. Therefore the Vector
object holds String or String[] types.

2. DefaultTableGenerator just set the elements from
DatabaseTable.uniqueConstraints() Vector to TableDefinition.uniqueKeysVector.

3. Therefore TableDefinition.uniqueKeys Vector can have String[] and String
type elements. Futhermore, UniqueKeyConstraint type object can be the
element of the Vector due to
TableDefinition.addUniqueConstraint(UniqueKeyConstraint)
method.
But TableDefinition.buildUniqueFieldTypes() assumes the element type of the
Vector as String[] so ClassCastException can be thrown.

Summary of the fix:
* Fixed metadata processors to handle columnNames properly and use
DatabaseTable.addUniqueConstraints(String[]).
* DatabaseTable
- Fixed the element type of unqueConstraints to be always String[].
- Removed addUniqueConstraint(String columnName) method, because it can used
carelessly and addUniqueConstraints(String[] columnNames) is enough.
* TableDefinition
- Fixed the element type of uniqueKeys to be always UniqueKeyConstraint
object.
- buildUniqueFieldTypes() is removed and now DefaultTableGenerator add
UniqueKeyConstraint object to TableDefinition from
DatabaseTable.uniqueConstraints Vector(whose element is now always String[]
columnNames).

Tests:
- Confirmed that the correct unique constraints are generated with newly
added models and tests.

Thanks
-Wonseok