You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/01/29 00:28:38 UTC
cvs commit: db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints ModelConstraints.java
tomdz 2004/01/28 15:28:38
Modified: src/xdoclet/java/src/xdoclet/modules/ojb/model
TorqueModelDef.java
src/xdoclet/test/xdoclet/modules/ojb/tests
CollectionTagIndirectionTableAttributeTests.java
RunAllTests.java
src/xdoclet/java/src/xdoclet/modules/ojb/constraints
ModelConstraints.java
Log:
Changed torque foreignkey generation for collections with indirection tables. Foreignkeys are now generated only when the collections are not inherited.
Revision Changes Path
1.2 +27 -15 db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/model/TorqueModelDef.java
Index: TorqueModelDef.java
===================================================================
RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/model/TorqueModelDef.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TorqueModelDef.java 25 Jan 2004 12:47:33 -0000 1.1
+++ TorqueModelDef.java 28 Jan 2004 23:28:38 -0000 1.2
@@ -320,18 +320,21 @@
CommaListIterator localKeys = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY));
CommaListIterator remoteKeys = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY));
FieldDescriptorDef fieldDef;
- ForeignkeyDef foreignkey;
+ ForeignkeyDef foreignkey = null;
ColumnDef columnDef;
String relationName;
String name;
int idx;
- relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME);
- foreignkey = tableDef.getForeignkey(relationName, ownerTable);
- if (foreignkey != null)
- {
- foreignkey = new ForeignkeyDef(relationName, ownerTable);
- tableDef.addForeignkey(foreignkey);
+ relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME);
+ if (relationName != null)
+ {
+ foreignkey = tableDef.getForeignkey(relationName, ownerTable);
+ if (foreignkey == null)
+ {
+ foreignkey = new ForeignkeyDef(relationName, ownerTable);
+ tableDef.addForeignkey(foreignkey);
+ }
}
for (idx = 0; localKeys.hasNext(); idx++)
{
@@ -347,15 +350,21 @@
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
- foreignkey.addColumnPair(name, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
+ if (foreignkey != null)
+ {
+ foreignkey.addColumnPair(name, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
+ }
}
- relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME);
- foreignkey = tableDef.getForeignkey(relationName, elementTable);
- if (foreignkey == null)
- {
- foreignkey = new ForeignkeyDef(relationName, elementTable);
- tableDef.addForeignkey(foreignkey);
+ relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME);
+ if (relationName != null)
+ {
+ foreignkey = tableDef.getForeignkey(relationName, elementTable);
+ if (foreignkey == null)
+ {
+ foreignkey = new ForeignkeyDef(relationName, elementTable);
+ tableDef.addForeignkey(foreignkey);
+ }
}
for (idx = 0; remoteKeys.hasNext(); idx++)
{
@@ -371,7 +380,10 @@
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE));
columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint());
- foreignkey.addColumnPair(name, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
+ if (foreignkey != null)
+ {
+ foreignkey.addColumnPair(name, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN));
+ }
}
}
1.5 +0 -6 db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/CollectionTagIndirectionTableAttributeTests.java
Index: CollectionTagIndirectionTableAttributeTests.java
===================================================================
RCS file: /home/cvs/db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/CollectionTagIndirectionTableAttributeTests.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CollectionTagIndirectionTableAttributeTests.java 25 Jan 2004 12:47:33 -0000 1.4
+++ CollectionTagIndirectionTableAttributeTests.java 28 Jan 2004 23:28:38 -0000 1.5
@@ -938,12 +938,6 @@
" type=\"VARCHAR\"\n"+
" size=\"24\"\n"+
" />\n"+
- " <foreign-key foreignTable=\"A\">\n"+
- " <reference local=\"AID\" foreign=\"id\"/>\n"+
- " </foreign-key>\n"+
- " <foreign-key foreignTable=\"B\">\n"+
- " <reference local=\"BID\" foreign=\"id\"/>\n"+
- " </foreign-key>\n"+
" </table>\n"+
" <table name=\"B\">\n"+
" <column name=\"id\"\n"+
1.9 +4 -4 db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/RunAllTests.java
Index: RunAllTests.java
===================================================================
RCS file: /home/cvs/db-ojb/src/xdoclet/test/xdoclet/modules/ojb/tests/RunAllTests.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- RunAllTests.java 25 Jan 2004 12:47:33 -0000 1.8
+++ RunAllTests.java 28 Jan 2004 23:28:38 -0000 1.9
@@ -140,8 +140,8 @@
suite.addTest(new TestSuite(CollectionTagCollectionClassAttributeTests.class));
suite.addTest(new TestSuite(CollectionTagDocumentationAttributeTests.class));
suite.addTest(new TestSuite(CollectionTagElementClassRefAttributeTests.class));
- suite.addTest(new TestSuite(CollectionTagForeignkeyAttributeTests.class));*/
- suite.addTest(new TestSuite(CollectionTagIndirectionTableAttributeTests.class));/*
+ suite.addTest(new TestSuite(CollectionTagForeignkeyAttributeTests.class));
+ suite.addTest(new TestSuite(CollectionTagIndirectionTableAttributeTests.class));
suite.addTest(new TestSuite(CollectionTagOrderbyAttributeTests.class));
suite.addTest(new TestSuite(CollectionTagOtmDependentAttributeTests.class));
suite.addTest(new TestSuite(CollectionTagProxyAttributeTests.class));
@@ -170,8 +170,8 @@
suite.addTest(new TestSuite(ModifyInheritedTagNullableAttributeTests.class));
suite.addTest(new TestSuite(ModifyInheritedTagOrderbyAttributeTests.class));
suite.addTest(new TestSuite(ModifyInheritedTagOtmDependentAttributeTests.class));
- suite.addTest(new TestSuite(ModifyInheritedTagPrecisionAndScaleAttributesTests.class));
- suite.addTest(new TestSuite(ModifyInheritedTagPrimarykeyAttributeTests.class));
+ suite.addTest(new TestSuite(ModifyInheritedTagPrecisionAndScaleAttributesTests.class));*/
+ suite.addTest(new TestSuite(ModifyInheritedTagPrimarykeyAttributeTests.class));/*
suite.addTest(new TestSuite(ModifyInheritedTagProxyAttributeTests.class));
suite.addTest(new TestSuite(ModifyInheritedTagQueryCustomizerAttributeTests.class));
suite.addTest(new TestSuite(ModifyInheritedTagRefreshAttributeTests.class));
1.3 +97 -47 db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java
Index: ModelConstraints.java
===================================================================
RCS file: /home/cvs/db-ojb/src/xdoclet/java/src/xdoclet/modules/ojb/constraints/ModelConstraints.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ModelConstraints.java 25 Jan 2004 12:47:33 -0000 1.2
+++ ModelConstraints.java 28 Jan 2004 23:28:38 -0000 1.3
@@ -133,7 +133,6 @@
*/
private void checkIndirectionTable(ModelDef modelDef, CollectionDescriptorDef collDef) throws ConstraintException
{
- String indirTable = collDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE);
String foreignkey = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY);
if ((foreignkey == null) || (foreignkey.length() == 0))
@@ -143,18 +142,8 @@
// we know that the class is present because the collection constraints have been checked already
ClassDescriptorDef elementClass = modelDef.getClass(collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF));
- CollectionDescriptorDef remoteCollDef = null;
+ CollectionDescriptorDef remoteCollDef = findRemoteCollection(collDef);
- // find the collection in the element class that has the same indirection table
- for (Iterator it = elementClass.getCollections(); it.hasNext();)
- {
- remoteCollDef = (CollectionDescriptorDef)it.next();
- if (indirTable.equals(remoteCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE)))
- {
- break;
- }
- remoteCollDef = null;
- }
if (remoteCollDef == null)
{
// error if there is none and we don't have remote-foreignkey specified
@@ -162,8 +151,6 @@
{
throw new ConstraintException("The collection "+collDef.getName()+" in class "+collDef.getOwner().getName()+" must specify remote-foreignkeys as the class on the other side of the m:n association has no corresponding collection");
}
- // we generate a name for the m:n relation that is unique across inheritance
- collDef.setProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME, collDef.getName());
}
else
{
@@ -184,17 +171,39 @@
// ensure the remote-foreignkey setting
collDef.setProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY, remoteKeys2);
}
+ }
+ // for torque we generate names for the m:n relation that are unique across inheritance
+ // but only if we don't have inherited collections
+ if (collDef.getOriginal() != null)
+ {
+ CollectionDescriptorDef origDef = (CollectionDescriptorDef)collDef.getOriginal();
+ CollectionDescriptorDef origRemoteDef = findRemoteCollection(origDef);
- // we generate names for the m:n relation that are unique across inheritance
- if (!collDef.hasProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME))
+ // we're removing any torque relation name properties from the base collection
+ origDef.setProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME, null);
+ origDef.setProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME, null);
+ if (origRemoteDef != null)
+ {
+ origRemoteDef.setProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME, null);
+ origRemoteDef.setProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME, null);
+ }
+ }
+ else if (!collDef.hasProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME))
+ {
+ if (remoteCollDef == null)
+ {
+ collDef.setProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME, collDef.getName());
+ collDef.setProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME, "inverse "+collDef.getName());
+ }
+ else
{
String relName = collDef.getName()+"-"+remoteCollDef.getName();
collDef.setProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME, relName);
remoteCollDef.setProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME, relName);
-
+
relName = remoteCollDef.getName()+"-"+collDef.getName();
-
+
collDef.setProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME, relName);
remoteCollDef.setProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME, relName);
}
@@ -202,6 +211,31 @@
}
/**
+ * Tries to find the corresponding remote collection for the given m:n collection.
+ *
+ * @param collDef The collection
+ * @return The corresponding remote collection
+ */
+ private CollectionDescriptorDef findRemoteCollection(CollectionDescriptorDef collDef)
+ {
+ ModelDef modelDef = (ModelDef)collDef.getOwner().getOwner();
+ ClassDescriptorDef elementClass = modelDef.getClass(collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF));
+ String indirTable = collDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE);
+ CollectionDescriptorDef remoteCollDef = null;
+
+ // find the collection in the element class that has the same indirection table
+ for (Iterator it = elementClass.getCollections(); it.hasNext();)
+ {
+ remoteCollDef = (CollectionDescriptorDef)it.next();
+ if (indirTable.equals(remoteCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE)))
+ {
+ return remoteCollDef;
+ }
+ }
+ return null;
+ }
+
+ /**
* Checks the foreignkeys of the collection.
*
* @param modelDef The model
@@ -465,34 +499,39 @@
}
else
{
- throw new ConstraintException("Cannot remove the primarykey status of field "+keyDef.getName()+" in class "+
- keyDef.getOwner().getName()+" as it is used in class "+
+ throw new ConstraintException("Cannot change the primarykey status of field "+keyDef.getName()+" in class "+
+ keyDef.getOwner().getName()+" as primarykeys are used in class "+
baseFieldDef.getOwner().getName()+" by the reference "+usingFeature.getName()+
" from class "+usingFeature.getOwner().getName());
}
}
- if (isIgnored || changesJdbcType)
+
+ usingFeature = usedByCollection(modelDef, baseFieldDef, changesPrimary);
+ if (usingFeature != null)
{
- // primary key change is irrelevant for the collection test
- usingFeature = usedByCollection(modelDef, baseFieldDef);
- if (usingFeature != null)
+ if (isIgnored)
{
- if (isIgnored)
- {
- throw new ConstraintException("Cannot ignore field "+keyDef.getName()+" in class "+keyDef.getOwner().getName()+
- " because it is used in class "+baseFieldDef.getOwner().getName()+
- " as a foreignkey of the collection "+usingFeature.getName()+" from class "+
- usingFeature.getOwner().getName());
- }
- else
- {
- throw new ConstraintException("Modification of the jdbc-type for the field "+keyDef.getName()+" in class "+
- keyDef.getOwner().getName()+" is not allowed because it is used in class "+
- baseFieldDef.getOwner().getName()+" as a foreignkey of the collecton "+
- usingFeature.getName()+" from class "+usingFeature.getOwner().getName());
- }
+ throw new ConstraintException("Cannot ignore field "+keyDef.getName()+" in class "+keyDef.getOwner().getName()+
+ " because it is used in class "+baseFieldDef.getOwner().getName()+
+ " as a foreignkey of the collection "+usingFeature.getName()+" from class "+
+ usingFeature.getOwner().getName());
+ }
+ else if (changesJdbcType)
+ {
+ throw new ConstraintException("Modification of the jdbc-type for the field "+keyDef.getName()+" in class "+
+ keyDef.getOwner().getName()+" is not allowed because it is used in class "+
+ baseFieldDef.getOwner().getName()+" as a foreignkey of the collecton "+
+ usingFeature.getName()+" from class "+usingFeature.getOwner().getName());
+ }
+ else
+ {
+ throw new ConstraintException("Cannot change the primarykey status of field "+keyDef.getName()+" in class "+
+ keyDef.getOwner().getName()+" as primarykeys are used in class "+
+ baseFieldDef.getOwner().getName()+" by the collection "+usingFeature.getName()+
+ " from class "+usingFeature.getOwner().getName());
}
}
+
baseFieldDef = (FieldDescriptorDef)baseFieldDef.getOriginal();
}
while (baseFieldDef != null);
@@ -503,15 +542,17 @@
* Checks whether the given field definition is used as a remote-foreignkey in an m:n
* association where the class owning the field has no collection for the association.
*
- * @param modelDef The model
- * @param fieldDef The current field descriptor def
+ * @param modelDef The model
+ * @param fieldDef The current field descriptor def
+ * @param elementClassSuffices Whether it suffices that the owner class of the field is an
+ * element class of a collection (for primary key tests)
* @return The collection that uses the field or <code>null</code> if the field is not
* used in this way
*/
- private CollectionDescriptorDef usedByCollection(ModelDef modelDef, FieldDescriptorDef fieldDef)
+ private CollectionDescriptorDef usedByCollection(ModelDef modelDef, FieldDescriptorDef fieldDef, boolean elementClassSuffices)
{
- ClassDescriptorDef ownerClass = (ClassDescriptorDef)fieldDef.getOwner();
- String name = fieldDef.getName();
+ ClassDescriptorDef ownerClass = (ClassDescriptorDef)fieldDef.getOwner();
+ String name = fieldDef.getName();
ClassDescriptorDef classDef;
CollectionDescriptorDef collDef;
@@ -523,11 +564,20 @@
collDef = (CollectionDescriptorDef)collIt.next();
// if the owner class of the field is the element class of a normal collection
// and the field is a foreignkey of this collection
- if (ownerClass.getName().equals(collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF)) &&
- !collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE) &&
- new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)).contains(name))
+ if (ownerClass.getName().equals(collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF)))
{
- return collDef;
+ if (collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
+ {
+ if (elementClassSuffices)
+ {
+ return collDef;
+ }
+ }
+ else if (new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)).contains(name))
+ {
+ // if the field is a foreignkey of this normal 1:n collection
+ return collDef;
+ }
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org