User: dsundstrom
Date: 01/12/01 13:55:06
Modified: src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata
JDBCRelationMetaData.java
JDBCRelationshipRoleMetaData.java
Log:
Added support for autogenerated foriegn key constraints on relationships.
Just add <fk-constraint>true</fk-constraint> to defaults or relationship
role.
Changed relation tables to synchronize data after jdbc store executes.
Revision Changes Path
1.8 +6 -2
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCRelationMetaData.java
Index: JDBCRelationMetaData.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCRelationMetaData.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- JDBCRelationMetaData.java 2001/11/28 11:32:55 1.7
+++ JDBCRelationMetaData.java 2001/12/01 21:55:05 1.8
@@ -25,7 +25,7 @@
* have set methods.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a>
- * @version $Revision: 1.7 $
+ * @version $Revision: 1.8 $
*/
public final class JDBCRelationMetaData {
private final static int TABLE = 1;
@@ -214,11 +214,13 @@
left = new JDBCRelationshipRoleMetaData(
this,
jdbcApplication,
+ element,
defaultValues.getLeftRelationshipRole());
right = new JDBCRelationshipRoleMetaData(
this,
jdbcApplication,
+ element,
defaultValues.getRightRelationshipRole());
return;
@@ -299,7 +301,7 @@
} else {
primaryKeyConstraint = defaultValues.hasPrimaryKeyConstraint();
}
-
+
// read-only
String readOnlyString = MetaData.getOptionalChildContent(
mappingElement, "read-only");
@@ -365,6 +367,7 @@
leftRole = new JDBCRelationshipRoleMetaData(
this,
jdbcApplication,
+ element,
defaultValues.getLeftRelationshipRole());
}
@@ -374,6 +377,7 @@
rightRole = new JDBCRelationshipRoleMetaData(
this,
jdbcApplication,
+ element,
defaultValues.getRightRelationshipRole());
}
1.10 +132 -116
jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCRelationshipRoleMetaData.java
Index: JDBCRelationshipRoleMetaData.java
===================================================================
RCS file:
/cvsroot/jboss/jboss/src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata/JDBCRelationshipRoleMetaData.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- JDBCRelationshipRoleMetaData.java 2001/11/26 03:12:27 1.9
+++ JDBCRelationshipRoleMetaData.java 2001/12/01 21:55:05 1.10
@@ -18,11 +18,11 @@
import org.w3c.dom.Element;
/**
- * Imutable class which represents one ejb-relationship-role element found in the
- * ejb-jar.xml file's ejb-relation elements.
+ * Imutable class which represents one ejb-relationship-role element found in
+ * the ejb-jar.xml file's ejb-relation elements.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a>
- * @version $Revision: 1.9 $
+ * @version $Revision: 1.10 $
*/
public final class JDBCRelationshipRoleMetaData {
/**
@@ -41,6 +41,11 @@
private final boolean multiplicityOne;
/**
+ * Should this role have a foreign key constraint?
+ */
+ private final boolean foreignKeyConstraint;
+
+ /**
* Should this entity be deleted when related entity is deleted.
*/
private final boolean cascadeDelete;
@@ -66,7 +71,7 @@
public JDBCRelationshipRoleMetaData(
JDBCRelationMetaData relationMetaData,
JDBCApplicationMetaData application,
- RelationshipRoleMetaData relationshipRole) throws DeploymentException {
+ RelationshipRoleMetaData relationshipRole) throws DeploymentException {
this.relationMetaData = relationMetaData;
RelationshipRoleMetaData relatedRole =
@@ -75,6 +80,7 @@
relationshipRoleName = relationshipRole.getRelationshipRoleName();
multiplicityOne = relationshipRole.isMultiplicityOne();
cascadeDelete = relationshipRole.isCascadeDelete();
+ foreignKeyConstraint = false;
String tempCmrFieldName = relationshipRole.getCMRFieldName();
if(tempCmrFieldName == null) {
@@ -83,7 +89,6 @@
relatedRole.getCMRFieldName();
}
cmrFieldName = tempCmrFieldName;
-
cmrFieldType = relationshipRole.getCMRFieldType();
// get the entity for this role
@@ -95,83 +100,10 @@
}
if(relationMetaData.isTableMappingStyle()) {
- // load all pks of entity into tableKeys map
- for(Iterator i = entity.getCMPFields().iterator(); i.hasNext(); ) {
- JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next();
- if(cmpField.isPrimaryKeyMember()) {
- cmpField = new JDBCCMPFieldMetaData(
- entity,
- cmpField,
- getCMRFieldName() + "_" + cmpField.getFieldName(),
- false);
- tableKeyFields.put(cmpField.getFieldName(), cmpField);
- }
- }
+ loadTableKeyFields();
} else if(relatedRole.isMultiplicityOne()){
- // get the related entity
- String relatedEntityName =
- relatedRole.getEntityName();
- JDBCEntityMetaData relatedEntity =
- application.getBeanByEjbName(relatedEntityName);
- if(relatedEntity == null) {
- throw new DeploymentException("Entity: " + relatedEntityName +
- " not found for: " + relationshipRoleName);
- }
-
- // load all pks of related entity into foreignKeys map
- for(Iterator i = relatedEntity.getCMPFields().iterator();
- i.hasNext();
- ) {
-
- JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next();
- if(cmpField.isPrimaryKeyMember()) {
- cmpField = new JDBCCMPFieldMetaData(
- entity,
- cmpField,
- getCMRFieldName() + "_" + cmpField.getFieldName(),
- false);
- foreignKeyFields.put(cmpField.getFieldName(), cmpField);
- }
- }
- }
- }
-
- public JDBCRelationshipRoleMetaData(
- JDBCRelationMetaData relationMetaData,
- JDBCApplicationMetaData application,
- JDBCRelationshipRoleMetaData defaultValues) throws DeploymentException {
-
- this.relationMetaData = relationMetaData;
-
- entity = application.getBeanByEjbName(defaultValues.getEntity().getName());
-
- relationshipRoleName = defaultValues.getRelationshipRoleName();
- multiplicityOne = defaultValues.isMultiplicityOne();
- cascadeDelete = defaultValues.isCascadeDelete();
-
- cmrFieldName = defaultValues.getCMRFieldName();
- cmrFieldType = defaultValues.getCMRFieldType();
-
- if(relationMetaData.isTableMappingStyle()) {
- // load all pks of entity into tableKeys map
- for(Iterator i = entity.getCMPFields().iterator(); i.hasNext(); ) {
- JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next();
- if(cmpField.isPrimaryKeyMember()) {
- cmpField = new JDBCCMPFieldMetaData(entity, cmpField,
getCMRFieldName() + cmpField.getFieldName(), false);
- tableKeyFields.put(cmpField.getFieldName(), cmpField);
- }
- }
- } else if(defaultValues.getRelatedRole().isMultiplicityOne()){
- // load all pks of related entity into foreignKeys map
- String relatedEntityName =
defaultValues.getRelatedRole().getEntity().getName();
- JDBCEntityMetaData relatedEntity =
application.getBeanByEjbName(relatedEntityName);
- for(Iterator i = relatedEntity.getCMPFields().iterator(); i.hasNext(); ) {
- JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next();
- if(cmpField.isPrimaryKeyMember()) {
- cmpField = new JDBCCMPFieldMetaData(entity, cmpField,
getCMRFieldName() + "_" + cmpField.getFieldName(), false);
- foreignKeyFields.put(cmpField.getFieldName(), cmpField);
- }
- }
+ String relatedEntityName = relatedRole.getEntityName();
+ loadForeignKeyFields(application.getBeanByEjbName(relatedEntityName));
}
}
@@ -179,10 +111,12 @@
JDBCRelationMetaData relationMetaData,
JDBCApplicationMetaData application,
Element element,
- JDBCRelationshipRoleMetaData defaultValues) throws DeploymentException {
+ JDBCRelationshipRoleMetaData defaultValues)
+ throws DeploymentException {
this.relationMetaData = relationMetaData;
- this.entity =
application.getBeanByEjbName(defaultValues.getEntity().getName());
+ this.entity = application.getBeanByEjbName(
+ defaultValues.getEntity().getName());
relationshipRoleName = defaultValues.getRelationshipRoleName();
multiplicityOne = defaultValues.isMultiplicityOne();
@@ -191,14 +125,35 @@
cmrFieldName = defaultValues.getCMRFieldName();
cmrFieldType = defaultValues.getCMRFieldType();
+ // foreign key constraint? If not provided, keep default.
+ String fkString = MetaData.getOptionalChildContent(
+ element, "fk-constraint");
+ if(fkString != null) {
+ foreignKeyConstraint = Boolean.valueOf(fkString).booleanValue();
+ } else {
+ foreignKeyConstraint = defaultValues.hasForeignKeyConstraint();
+ }
+
if(relationMetaData.isTableMappingStyle()) {
- loadTableKeyFields(element);
- } else if(defaultValues.getRelatedRole().isMultiplicityOne()){
- String relatedEntityName =
defaultValues.getRelatedRole().getEntity().getName();
- loadForeignKeyFields(element,
application.getBeanByEjbName(relatedEntityName));
+ if("defaults".equals(element.getTagName())) {
+ loadTableKeyFields();
+ } else {
+ loadTableKeyFields(element);
+ }
+ } else if(defaultValues.getRelatedRole().isMultiplicityOne()) {
+ String relatedEntityName =
+ defaultValues.getRelatedRole().getEntity().getName();
+ JDBCEntityMetaData relatedEntity =
+ application.getBeanByEjbName(relatedEntityName);
+
+ if("defaults".equals(element.getTagName())) {
+ loadForeignKeyFields(relatedEntity);
+ } else {
+ loadForeignKeyFields(element, relatedEntity);
+ }
}
}
-
+
/**
* Gets the relation to which this role belongs.
*/
@@ -214,6 +169,15 @@
}
/**
+ * Should this role use a foreign key constraint.
+ * @return true if the store mananager will execute an ALTER TABLE ADD
+ * CONSTRAINT statement to add a foreign key constraint.
+ */
+ public boolean hasForeignKeyConstraint() {
+ return foreignKeyConstraint;
+ }
+
+ /**
* Checks if the multiplicity is one.
*/
public boolean isMultiplicityOne() {
@@ -229,7 +193,8 @@
/**
* Is this field single valued, that means it does not return a collection.
- * A relationship role is single valued if the related role has a multiplicity
of one.
+ * A relationship role is single valued if the related role has a
+ * multiplicity of one.
* @return true if this role does not return a collection
*/
public boolean isSingleValued() {
@@ -238,7 +203,8 @@
/**
* Is this field collection valued, that means it returns a collection.
- * A relationship role is collection valued if the related role has a
multiplicity of many.
+ * A relationship role is collection valued if the related role has a
+ * multiplicity of many.
* @return true if this role returns a collection
*/
public boolean isCollectionValued() {
@@ -292,30 +258,53 @@
}
/**
- * Gets the key fields of this role in the relation table. The table key fields
- * hold the primary keys of this role's entity. A relationship role has table
key
- * fields if the relation is mapped to a relation table.
+ * Gets the key fields of this role in the relation table. The table key
+ * fields hold the primary keys of this role's entity. A relationship role
+ * has table key fields if the relation is mapped to a relation table.
* @return an unmodifiable collection of JDBCCMPFieldMetaData objects
*/
public Collection getTableKeyFields() {
return Collections.unmodifiableCollection(tableKeyFields.values());
}
-
+
/**
- * Loads the foreign key fields for this role based on the primary keys of the
- * specified related entity and the override data from the xml element.
+ * Loads the foreign key fields for this role based on the primary keys of
+ * the specified related entity.
*/
- private void loadForeignKeyFields(Element element, JDBCEntityMetaData
relatedEntity) throws DeploymentException {
- // load all pks of related entity into foreignKeys map
- for(Iterator i = relatedEntity.getCMPFields().iterator(); i.hasNext(); ) {
+ private void loadForeignKeyFields(JDBCEntityMetaData relatedEntity)
+ throws DeploymentException {
+
+ if(relatedEntity == null) {
+ throw new DeploymentException("Entity: Related entity not found " +
+ "for: " + relationshipRoleName);
+ }
+
+ for(Iterator i = relatedEntity.getCMPFields().iterator(); i.hasNext();) {
JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next();
+
if(cmpField.isPrimaryKeyMember()) {
- cmpField = new JDBCCMPFieldMetaData(entity, cmpField, getCMRFieldName()
+ "_" + cmpField.getFieldName(), false);
+ cmpField = new JDBCCMPFieldMetaData(
+ entity,
+ cmpField,
+ getCMRFieldName() + "_" + cmpField.getFieldName(),
+ false);
foreignKeyFields.put(cmpField.getFieldName(), cmpField);
}
}
+ }
+
+ /**
+ * Loads the foreign key fields for this role based on the primary keys of
+ * the specified related entity and the override data from the xml element.
+ */
+ private void loadForeignKeyFields(
+ Element element,
+ JDBCEntityMetaData relatedEntity) throws DeploymentException {
+
+ loadForeignKeyFields(relatedEntity);
- Element foreignKeysElement =
MetaData.getOptionalChild(element,"foreign-key-fields");
+ Element foreignKeysElement = MetaData.getOptionalChild(
+ element,"foreign-key-fields");
// no field overrides, we're done
if(foreignKeysElement == null) {
@@ -323,7 +312,8 @@
}
// load overrides
- Iterator fkIter = MetaData.getChildrenByTagName(foreignKeysElement,
"foreign-key-field");
+ Iterator fkIter = MetaData.getChildrenByTagName(
+ foreignKeysElement, "foreign-key-field");
// if empty foreign-key-fields element, no fk should be used
if(!fkIter.hasNext()) {
@@ -332,46 +322,72 @@
while(fkIter.hasNext()) {
Element foreignKeyElement = (Element)fkIter.next();
- String foreignKeyName = MetaData.getUniqueChildContent(foreignKeyElement,
"field-name");
- JDBCCMPFieldMetaData cmpField =
(JDBCCMPFieldMetaData)foreignKeyFields.get(foreignKeyName);
+ String foreignKeyName = MetaData.getUniqueChildContent(
+ foreignKeyElement, "field-name");
+ JDBCCMPFieldMetaData cmpField =
+ (JDBCCMPFieldMetaData)foreignKeyFields.get(foreignKeyName);
if(cmpField == null) {
- throw new DeploymentException("CMP field for foreign key not found:
field name="+foreignKeyName);
+ throw new DeploymentException(
+ "CMP field for foreign key not found: field name=" +
+ foreignKeyName);
}
- cmpField = new JDBCCMPFieldMetaData(entity, foreignKeyElement, cmpField,
false);
+ cmpField = new JDBCCMPFieldMetaData(
+ entity, foreignKeyElement, cmpField, false);
foreignKeyFields.put(cmpField.getFieldName(), cmpField);
}
}
/**
* Loads the table key fields for this role based on the primary keys of the
- * this entity and the override data from the xml element.
+ * this entity.
*/
- private void loadTableKeyFields(Element element) throws DeploymentException {
- // load all pks of entity into tableKeys map
+ private void loadTableKeyFields() {
+
for(Iterator i = entity.getCMPFields().iterator(); i.hasNext(); ) {
JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next();
+
if(cmpField.isPrimaryKeyMember()) {
- cmpField = new JDBCCMPFieldMetaData(entity, cmpField, getCMRFieldName()
+ "_" + cmpField.getFieldName(), false);
+ cmpField = new JDBCCMPFieldMetaData(
+ entity,
+ cmpField,
+ getCMRFieldName() + "_" + cmpField.getFieldName(),
+ false);
tableKeyFields.put(cmpField.getFieldName(), cmpField);
}
}
+ }
- Element tableKeysElement =
MetaData.getOptionalChild(element,"table-key-fields");
+ /**
+ * Loads the table key fields for this role based on the primary keys of the
+ * this entity and the override data from the xml element.
+ */
+ private void loadTableKeyFields(Element element) throws DeploymentException {
+ loadTableKeyFields();
+ Element tableKeysElement = MetaData.getOptionalChild(
+ element,"table-key-fields");
+
// no field overrides, we're done
if(tableKeysElement == null) {
return;
}
// load overrides
- for(Iterator i = MetaData.getChildrenByTagName(tableKeysElement,
"table-key-field"); i.hasNext(); ) {
+ for(Iterator i = MetaData.getChildrenByTagName(
+ tableKeysElement, "table-key-field"); i.hasNext(); ) {
+
Element tableKeyElement = (Element)i.next();
- String tableKeyName = MetaData.getUniqueChildContent(tableKeyElement,
"field-name");
- JDBCCMPFieldMetaData cmpField =
(JDBCCMPFieldMetaData)tableKeyFields.get(tableKeyName);
+ String tableKeyName = MetaData.getUniqueChildContent(
+ tableKeyElement, "field-name");
+ JDBCCMPFieldMetaData cmpField =
+ (JDBCCMPFieldMetaData)tableKeyFields.get(tableKeyName);
if(cmpField == null) {
- throw new DeploymentException("CMP field for table key not found: field
name="+tableKeyName);
+ throw new DeploymentException(
+ "CMP field for table key not found: field name=" +
+ tableKeyName);
}
- cmpField = new JDBCCMPFieldMetaData(entity, tableKeyElement, cmpField,
false);
+ cmpField = new JDBCCMPFieldMetaData(
+ entity, tableKeyElement, cmpField, false);
tableKeyFields.put(cmpField.getFieldName(), cmpField);
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development