User: dsundstrom Date: 02/01/31 17:43:52 Modified: src/main/org/jboss/ejb/plugins/cmp/jdbc/metadata JDBCRelationMetaData.java JDBCRelationshipRoleMetaData.java Log: Moved ejb-relationship-role element outside of the mapping element. Changed foreign-keys-fields and table-key-fields fields to the generic key-fields. Revision Changes Path 1.10 +148 -131 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.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- JDBCRelationMetaData.java 2001/12/05 05:07:17 1.9 +++ JDBCRelationMetaData.java 2002/02/01 01:43:52 1.10 @@ -25,7 +25,7 @@ * have set methods. * * @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a> - * @version $Revision: 1.9 $ + * @version $Revision: 1.10 $ */ public final class JDBCRelationMetaData { private final static int TABLE = 1; @@ -58,7 +58,7 @@ private transient DataSource dataSource; /** type mapping used for the relation table */ - private final JDBCTypeMappingMetaData typeMapping; + private final JDBCTypeMappingMetaData datasourceMapping; /** the name of the table to use for this bean */ private final String tableName; @@ -111,7 +111,7 @@ } dataSourceName = null; - typeMapping = null; + datasourceMapping = null; createTable = false; removeTable = false; rowLocking = false; @@ -128,7 +128,9 @@ this, jdbcApplication, rightRole); - + left.init(right); + right.init(left); + if(mappingStyle == TABLE) { tableName = createDefaultTableName(); } else { @@ -156,76 +158,14 @@ JDBCRelationMetaData defaultValues) throws DeploymentException { relationName = defaultValues.getRelationName(); - - // get the mapping element; may be the defaults, table-mapping, or - // foreign-key-mapping - Element mappingElement; - if("defaults".equals(element.getTagName())) { - mappingElement = element; - - // set mapping style based on perferred-relation-mapping (if possible) - String perferredRelationMapping = MetaData.getOptionalChildContent( - element, "preferred-relation-mapping"); - - if("table".equals(perferredRelationMapping) || - defaultValues.isManyToMany()) { - mappingStyle = TABLE; - } else { - mappingStyle = FOREIGN_KEY; - } - } else { - // check for table mapping style - mappingElement = MetaData.getOptionalChild(element, "table-mapping"); - if(mappingElement != null) { - mappingStyle = TABLE; - } else { - // check for foreign key mapping - mappingElement = MetaData.getOptionalChild( - element, "foreign-key-mapping"); - if(mappingElement != null) { - mappingStyle = FOREIGN_KEY; - if(defaultValues.isManyToMany()) { - throw new DeploymentException("Foreign key mapping-style " + - "is not allowed for many-to-many relationsips."); - } - } else { - // no mapping style element, will use defaultValues - if(defaultValues.isForeignKeyMappingStyle()) { - mappingStyle = FOREIGN_KEY; - } else { - mappingStyle = TABLE; - } - } - } - } - - // if no mapping element given, use defaultValues - if(mappingElement == null) { - dataSourceName = defaultValues.getDataSourceName(); - typeMapping = defaultValues.getTypeMapping(); - tableName = defaultValues.getTableName(); - createTable = defaultValues.getCreateTable(); - removeTable = defaultValues.getRemoveTable(); - rowLocking = defaultValues.hasRowLocking(); - primaryKeyConstraint = defaultValues.hasPrimaryKeyConstraint(); - readOnly = defaultValues.isReadOnly(); - readTimeOut = defaultValues.getReadTimeOut(); - - left = new JDBCRelationshipRoleMetaData( - this, - jdbcApplication, - element, - defaultValues.getLeftRelationshipRole()); - - right = new JDBCRelationshipRoleMetaData( - this, - jdbcApplication, - element, - defaultValues.getRightRelationshipRole()); - - return; - } + mappingStyle = loadMappingStyle(element, defaultValues); + // + // Load all of the table options. defaults and relation-table-mapping + // will have these elements, and foreign-key will get the default values. + // + Element mappingElement = getMappingElement(element); + // datasource name String dataSourceNameString = MetaData.getOptionalChildContent( mappingElement, "datasource"); @@ -237,18 +177,19 @@ // get the type mapping for this datasource (optional, but always // set in standardjbosscmp-jdbc.xml) - String typeMappingString = MetaData.getOptionalChildContent( - mappingElement, "type-mapping"); - if(typeMappingString != null) { - typeMapping = jdbcApplication.getTypeMappingByName( - typeMappingString); + String datasourceMappingString = MetaData.getOptionalChildContent( + mappingElement, "datasource-mapping"); + if(datasourceMappingString != null) { + datasourceMapping = jdbcApplication.getTypeMappingByName( + datasourceMappingString); - if(typeMapping == null) { + if(datasourceMapping == null) { throw new DeploymentException("Error in jbosscmp-jdbc.xml : " + - "type-mapping " + typeMappingString + " not found"); + "datasource-mapping " + datasourceMappingString + + " not found"); } } else { - typeMapping = defaultValues.getTypeMapping(); + datasourceMapping = defaultValues.getTypeMapping(); } // get table name @@ -323,67 +264,143 @@ // // load metadata for each specified role // - String leftRoleName = - defaultValues.getLeftRelationshipRole().getRelationshipRoleName(); - String rightRoleName = - defaultValues.getRightRelationshipRole().getRelationshipRoleName(); - JDBCRelationshipRoleMetaData leftRole = null; - JDBCRelationshipRoleMetaData rightRole = null; + JDBCRelationshipRoleMetaData defaultLeft = + defaultValues.getLeftRelationshipRole(); + JDBCRelationshipRoleMetaData defaultRight = + defaultValues.getRightRelationshipRole(); + if(!MetaData.getChildrenByTagName( + element, "ejb-relationship-role").hasNext()) { + + // no roles specified use the defaults + left = new JDBCRelationshipRoleMetaData( + this, + jdbcApplication, + element, + defaultLeft); + + right = new JDBCRelationshipRoleMetaData( + this, + jdbcApplication, + element, + defaultRight); + + left.init(right); + right.init(left); + } else { + Element leftElement = + getEJBRelationshipRoleElement(element, defaultLeft); + left = new JDBCRelationshipRoleMetaData( + this, + jdbcApplication, + leftElement, + defaultLeft); + + Element rightElement = + getEJBRelationshipRoleElement(element, defaultRight); + right = new JDBCRelationshipRoleMetaData( + this, + jdbcApplication, + rightElement, + defaultRight); + + left.init(right, leftElement); + right.init(left, rightElement); + } + } + + private int loadMappingStyle(Element element, + JDBCRelationMetaData defaultValues) throws DeploymentException { + + // if defaults check for preferred-relation-mapping + if("defaults".equals(element.getTagName())) { + // set mapping style based on perferred-relation-mapping (if possible) + String perferredRelationMapping = MetaData.getOptionalChildContent( + element, "preferred-relation-mapping"); + + if("relation-table".equals(perferredRelationMapping) || + defaultValues.isManyToMany()) { + return TABLE; + } else { + return FOREIGN_KEY; + } + } + + // check for table mapping style + if(MetaData.getOptionalChild(element, "relation-table-mapping") != null) { + return TABLE; + } + + // check for foreign-key mapping style + if(MetaData.getOptionalChild(element, "foreign-key-mapping") != null) { + if(defaultValues.isManyToMany()) { + throw new DeploymentException("Foreign key mapping-style " + + "is not allowed for many-to-many relationsips."); + } + return FOREIGN_KEY; + } + + // no mapping style element, will use defaultValues + return defaultValues.mappingStyle; + } + + private Element getMappingElement(Element element) + throws DeploymentException { + + // if defaults check for preferred-relation-mapping + if("defaults".equals(element.getTagName())) { + return element; + } + + // check for table mapping style + Element tableMappingElement = + MetaData.getOptionalChild(element, "relation-table-mapping"); + if(tableMappingElement != null) { + return tableMappingElement; + } + + // check for foreign-key mapping style + Element foreignKeyMappingElement = + MetaData.getOptionalChild(element, "foreign-key-mapping"); + if(foreignKeyMappingElement != null) { + return foreignKeyMappingElement; + } + return null; + } + + private Element getEJBRelationshipRoleElement( + Element element, + JDBCRelationshipRoleMetaData defaultRole) throws DeploymentException { + + String roleName = defaultRole.getRelationshipRoleName(); + Iterator iter = MetaData.getChildrenByTagName( - mappingElement, "ejb-relationship-role"); + element, "ejb-relationship-role"); + if(!iter.hasNext()) { + throw new DeploymentException("No ejb-relationship-role " + + "elements found"); + } + + Element roleElement = null; for(int i=0; iter.hasNext(); i++) { - // only 2 roles are allow if(i > 1) { throw new DeploymentException("Expected only 2 " + "ejb-relationship-role but found more then 2"); } - Element relationshipRoleElement = (Element)iter.next(); - String relationshipRoleName = MetaData.getUniqueChildContent( - relationshipRoleElement, "ejb-relationship-role-name"); - if(leftRoleName.equals(relationshipRoleName)) { - leftRole = new JDBCRelationshipRoleMetaData( - this, - jdbcApplication, - relationshipRoleElement, - defaultValues.getLeftRelationshipRole()); - } else if(rightRoleName.equals(relationshipRoleName)) { - rightRole = new JDBCRelationshipRoleMetaData( - this, - jdbcApplication, - relationshipRoleElement, - defaultValues.getRightRelationshipRole()); - } else { - throw new DeploymentException("Found ejb-relationship-role '" + - relationshipRoleName + "' in jboss-cmp.xml, but no " + - "matching role exits in ejb-jar.xml"); + Element tempElement = (Element)iter.next(); + if(roleName.equals(MetaData.getUniqueChildContent( + tempElement, "ejb-relationship-role-name"))) { + roleElement = tempElement; } - } - - // if left role was not specified create a new one for this relation - if(leftRole == null) { - leftRole = new JDBCRelationshipRoleMetaData( - this, - jdbcApplication, - element, - defaultValues.getLeftRelationshipRole()); - } - - // if right role was not specified create a new one for this relation - if(rightRole == null) { - rightRole = new JDBCRelationshipRoleMetaData( - this, - jdbcApplication, - element, - defaultValues.getRightRelationshipRole()); + + if(roleElement == null) { + throw new DeploymentException("An ejb-relationship-role element was " + + "not found for role '" + roleName + "'"); } - - // assign the final roles - left = leftRole; - right = rightRole; + return roleElement; } /** @@ -464,7 +481,7 @@ * @return the jdbc type mapping for this entity */ public JDBCTypeMappingMetaData getTypeMapping() { - return typeMapping; + return datasourceMapping; } /** 1.13 +102 -170 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.12 retrieving revision 1.13 diff -u -r1.12 -r1.13 --- JDBCRelationshipRoleMetaData.java 2002/01/15 21:16:24 1.12 +++ JDBCRelationshipRoleMetaData.java 2002/02/01 01:43:52 1.13 @@ -23,7 +23,7 @@ * the ejb-jar.xml file's ejb-relation elements. * * @author <a href="mailto:[EMAIL PROTECTED]">Dain Sundstrom</a> - * @version $Revision: 1.12 $ + * @version $Revision: 1.13 $ */ public final class JDBCRelationshipRoleMetaData { /** @@ -71,46 +71,37 @@ */ private final JDBCReadAheadMetaData readAhead; - private final Map tableKeyFields = new HashMap(); - private final Map foreignKeyFields = new HashMap(); + /** + * The other role in this relationship. + */ + private JDBCRelationshipRoleMetaData relatedRole; + + /** + * The key fields used by this role by field name. + */ + private Map keyFields; public JDBCRelationshipRoleMetaData( JDBCRelationMetaData relationMetaData, JDBCApplicationMetaData application, - RelationshipRoleMetaData relationshipRole) throws DeploymentException { + RelationshipRoleMetaData role) throws DeploymentException { this.relationMetaData = relationMetaData; - RelationshipRoleMetaData relatedRole = - relationshipRole.getRelatedRoleMetaData(); - relationshipRoleName = relationshipRole.getRelationshipRoleName(); - multiplicityOne = relationshipRole.isMultiplicityOne(); - cascadeDelete = relationshipRole.isCascadeDelete(); + relationshipRoleName = role.getRelationshipRoleName(); + multiplicityOne = role.isMultiplicityOne(); + cascadeDelete = role.isCascadeDelete(); foreignKeyConstraint = false; readAhead = null; - String tempCmrFieldName = relationshipRole.getCMRFieldName(); - if(tempCmrFieldName == null) { - // no cmr field on this side use relatedEntityName_relatedCMRFieldName - tempCmrFieldName = relatedRole.getEntityName() + "_" + - relatedRole.getCMRFieldName(); - } - cmrFieldName = tempCmrFieldName; - cmrFieldType = relationshipRole.getCMRFieldType(); + cmrFieldName = loadCMRFieldName(role); + cmrFieldType = role.getCMRFieldType(); // get the entity for this role - entity = application.getBeanByEjbName(relationshipRole.getEntityName()); + entity = application.getBeanByEjbName(role.getEntityName()); if(entity == null) { - throw new DeploymentException("Entity: " + - relationshipRole.getEntityName() + - " not found for: " + relationshipRoleName); - } - - if(relationMetaData.isTableMappingStyle()) { - loadTableKeyFields(); - } else if(relatedRole.isMultiplicityOne()){ - String relatedEntityName = relatedRole.getEntityName(); - loadForeignKeyFields(application.getBeanByEjbName(relatedEntityName)); + throw new DeploymentException("Entity: " + role.getEntityName() + + " not found for: " + role); } } @@ -150,25 +141,33 @@ } else { readAhead = entity.getReadAhead(); } + } - if(relationMetaData.isTableMappingStyle()) { - 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); + public void init(JDBCRelationshipRoleMetaData relatedRole) + throws DeploymentException { + init(relatedRole, null); + } - if("defaults".equals(element.getTagName())) { - loadForeignKeyFields(relatedEntity); - } else { - loadForeignKeyFields(element, relatedEntity); - } + public void init(JDBCRelationshipRoleMetaData relatedRole, Element element) + throws DeploymentException { + this.relatedRole = relatedRole; + if(element == null || "defaults".equals(element.getTagName())) { + keyFields = loadKeyFields(); + } else { + keyFields = loadKeyFields(element); + } + } + + private String loadCMRFieldName(RelationshipRoleMetaData role) { + String fieldName = role.getCMRFieldName(); + if(fieldName == null) { + // no cmr field on this side use relatedEntityName_relatedCMRFieldName + RelationshipRoleMetaData relatedRole = + role.getRelatedRoleMetaData(); + fieldName = relatedRole.getEntityName() + "_" + + relatedRole.getCMRFieldName(); } + return fieldName; } /** @@ -271,41 +270,27 @@ } /** - * Gets the foreign key fields of this role. The foreign key fields hold the - * primary keys of the related entity. A relationship role has foreign key - * fields if the relation mapping style is foreign key and the other side of - * the relationship has a multiplicity of one. + * Gets the key fields of this role. * @return an unmodifiable collection of JDBCCMPFieldMetaData objects */ - public Collection getForeignKeyFields() { - return Collections.unmodifiableCollection(foreignKeyFields.values()); + public Collection getKeyFields() { + return Collections.unmodifiableCollection(keyFields.values()); } - - /** - * 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. + * Loads the key fields for this role based on the primary keys of the + * this entity. */ - private void loadForeignKeyFields(JDBCEntityMetaData relatedEntity) - throws DeploymentException { - - if(relatedEntity == null) { - throw new DeploymentException("Entity: Related entity not found " + - "for: " + relationshipRoleName); + private Map loadKeyFields() { + // with foreign key mapping, the one side of one-to-many + // does not have key fields + if(relationMetaData.isForeignKeyMappingStyle() && isMultiplicityMany()) { + return Collections.EMPTY_MAP; } + // get all of the pk fields ArrayList pkFields = new ArrayList(); - - for(Iterator i = relatedEntity.getCMPFields().iterator(); i.hasNext();) { + for(Iterator i = entity.getCMPFields().iterator(); i.hasNext(); ) { JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next(); if(cmpField.isPrimaryKeyMember()) { @@ -313,93 +298,18 @@ } } + // generate a new key field for each pk field + Map fields = new HashMap(pkFields.size()); for(Iterator i = pkFields.iterator(); i.hasNext(); ) { JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next(); - String columnName = getCMRFieldName(); - if(pkFields.size() > 1) { - columnName += "_" + cmpField.getFieldName(); - } - - cmpField = new JDBCCMPFieldMetaData( - entity, - cmpField, - columnName, - false, - relationMetaData.isReadOnly(), - relationMetaData.getReadTimeOut()); - 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"); - - // no field overrides, we're done - if(foreignKeysElement == null) { - return; - } - - // load overrides - Iterator fkIter = MetaData.getChildrenByTagName( - foreignKeysElement, "foreign-key-field"); - - // if empty foreign-key-fields element, no fk should be used - if(!fkIter.hasNext()) { - foreignKeyFields.clear(); - } - - while(fkIter.hasNext()) { - Element foreignKeyElement = (Element)fkIter.next(); - 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); + String columnName; + if(relationMetaData.isTableMappingStyle()) { + columnName = entity.getName(); + } else { + columnName = relatedRole.getCMRFieldName(); } - cmpField = new JDBCCMPFieldMetaData( - entity, - foreignKeyElement, - cmpField, - false, - relationMetaData.isReadOnly(), - relationMetaData.getReadTimeOut()); - foreignKeyFields.put(cmpField.getFieldName(), cmpField); - } - } - /** - * Loads the table key fields for this role based on the primary keys of the - * this entity. - */ - private void loadTableKeyFields() { - ArrayList pkFields = new ArrayList(); - - for(Iterator i = entity.getCMPFields().iterator(); i.hasNext(); ) { - JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next(); - - if(cmpField.isPrimaryKeyMember()) { - pkFields.add(cmpField); - } - } - - for(Iterator i = pkFields.iterator(); i.hasNext(); ) { - JDBCCMPFieldMetaData cmpField = (JDBCCMPFieldMetaData)i.next(); - - String columnName = entity.getName(); if(pkFields.size() > 1) { columnName += "_" + cmpField.getFieldName(); } @@ -411,47 +321,69 @@ false, relationMetaData.isReadOnly(), relationMetaData.getReadTimeOut()); - tableKeyFields.put(cmpField.getFieldName(), cmpField); + fields.put(cmpField.getFieldName(), cmpField); } + return Collections.unmodifiableMap(fields); } /** - * Loads the table key fields for this role based on the primary keys of the + * Loads the 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(); + private Map loadKeyFields(Element element) + throws DeploymentException { - Element tableKeysElement = MetaData.getOptionalChild( - element,"table-key-fields"); + Element keysElement = + MetaData.getOptionalChild(element,"key-fields"); // no field overrides, we're done - if(tableKeysElement == null) { - return; + if(keysElement == null) { + return loadKeyFields(); } + + // load overrides + Iterator iter = MetaData.getChildrenByTagName(keysElement, "key-field"); + // if key-fields element empty, no key should be used + if(!iter.hasNext()) { + return Collections.EMPTY_MAP; + } else if(relationMetaData.isForeignKeyMappingStyle() + && isMultiplicityMany()) { + throw new DeploymentException("A role with multiplicity many using " + + "foreign-key mapping is not allowed to have key-fields"); + } + + // load the default field values + Map defaultFields = new HashMap(loadKeyFields()); + // load overrides - for(Iterator i = MetaData.getChildrenByTagName( - tableKeysElement, "table-key-field"); i.hasNext(); ) { + Map fields = new HashMap(defaultFields.size()); + while(iter.hasNext()) { + Element keyElement = (Element)iter.next(); + String fieldName = + MetaData.getUniqueChildContent(keyElement, "field-name"); - Element tableKeyElement = (Element)i.next(); - String tableKeyName = MetaData.getUniqueChildContent( - tableKeyElement, "field-name"); JDBCCMPFieldMetaData cmpField = - (JDBCCMPFieldMetaData)tableKeyFields.get(tableKeyName); + (JDBCCMPFieldMetaData)defaultFields.remove(fieldName); if(cmpField == null) { throw new DeploymentException( - "CMP field for table key not found: field name=" + - tableKeyName); + "CMP field for key not found: field name=" + fieldName); } cmpField = new JDBCCMPFieldMetaData( entity, - tableKeyElement, + keyElement, cmpField, false, relationMetaData.isReadOnly(), relationMetaData.getReadTimeOut()); - tableKeyFields.put(cmpField.getFieldName(), cmpField); + fields.put(cmpField.getFieldName(), cmpField); + } + + // all fields must be overriden + if(!defaultFields.isEmpty()) { + throw new DeploymentException("Mappings were not provided for all " + + "fields: unmaped fields=" + defaultFields.keySet()); } + return Collections.unmodifiableMap(fields); } }
_______________________________________________ Jboss-development mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/jboss-development