try
{
relationalMapping.setMetaclass(metaclass);
relationalMapping.setDataSource(dataSource);
XMLUtil.withFirstChildElement(persistenceMappingElement, "RelationalMapping", true, new ElementHandler()
{
public void handleElement(Element relationalMappingElement)
{
String sPrimaryTable = XMLUtil.getStringAttr(relationalMappingElement, "primaryTable");
relationalMapping.setPrimaryTable((sPrimaryTable == null) ? null : m_schema.getTable(sPrimaryTable));
final String sKeyGeneratorName = XMLUtil.getStringAttr(relationalMappingElement, "keyGenerator");
if (sKeyGeneratorName != null)
{
if (sKeyGeneratorName.equals("identity"))
{
relationalMapping.setKeyGenerator(RelationalMapping.KEY_GEN_IDENTITY);
}
else
{
m_loader.addComponentFixup(new ContextFixup(getHelper())
{
public void fixup()
{
relationalMapping.setKeyGenerator(
relationalMapping.getMetaclass().getMetadata()
.getComponent(sKeyGeneratorName));
}
});
}
}
XMLUtil.withFirstChildElement(relationalMappingElement, "AttributeMappings", false, new ElementHandler()
{
public void handleElement(Element attributeMappingsElement)
{
XMLUtil.forEachChildElement(attributeMappingsElement, "AttributeMapping",
getHelper().new ElementHandler("attribute")
{
public void handleElement(Element attributeMappingElement, String sAttributeName)
{
final Attribute attribute = metaclass.getAttribute(sAttributeName);
final String sTableName = XMLUtil.getStringAttr(attributeMappingElement, "table");
final String sColumnName = XMLUtil.getStringAttr(attributeMappingElement, "column");
final String sSourceKeyName = XMLUtil.getStringAttr(attributeMappingElement, "sourceKey");
final String sDestinationKeyName = XMLUtil.getStringAttr(attributeMappingElement, "destinationKey");
AttributeMapping mapping;
m_persistenceMappingElement = null;
XMLUtil.withFirstChildElement(attributeMappingElement,
"PersistenceMapping", false, m_persistenceMappingElementHandler);
if (attribute.getType().isPrimitive())
{
if (sColumnName == null || sSourceKeyName != null ||
sDestinationKeyName != null || m_persistenceMappingElement != null)
{
throw new MetadataException("err.meta.primitiveRelationalMapping",
new Object[]{attribute.getName(), metaclass.getName()});
}
if (attribute.isCollection())
{
throw new MetadataException("err.meta.unsupportedRelationalMapping",
new Object[]{attribute.getName(), metaclass.getName()});
}
RelationalPrimitiveMapping primitiveMapping = new RelationalPrimitiveMapping();
Table table = (sTableName == null) ? relationalMapping.getPrimaryTable() : m_schema.getTable(sTableName);
if (table == null)
{
throw new MetadataException("err.meta.unspecifiedRelationalMappingTable",
new Object[]{attribute.getName(), metaclass.getName()});
}
primitiveMapping.setColumn(table.getColumn(sColumnName));
primitiveMapping.setAttribute(attribute);
mapping = primitiveMapping;
}
else
{
if (sTableName != null || sColumnName != null ||
m_persistenceMappingElement != null &&
(sSourceKeyName != null || sDestinationKeyName != null))
{
throw new MetadataException("err.meta.classRelationalMapping",
new Object[]{attribute.getName(), metaclass.getName()});
}
final RelationalClassMapping classMapping = new RelationalClassMapping();
classMapping.setAttribute(attribute);
if (m_persistenceMappingElement != null)
{
if (attribute.isCollection())
{
throw new MetadataException("err.meta.unsupportedRelationalMapping",
new Object[]{attribute.getName(), metaclass.getName()});
}
classMapping.setSourceKey(relationalMapping.getPrimaryTable().getPrimaryKey());
classMapping.setMapping(m_loader.loadPersistenceMapping(m_persistenceMappingElement, (Metaclass)attribute.getType()));
}
else
{
if (sSourceKeyName != null)
{
classMapping.setSourceKey(m_schema.getIndex(sSourceKeyName));
}
else
{
Table primaryTable = relationalMapping.getPrimaryTable();
if (primaryTable != null)
{
classMapping.setSourceKey(primaryTable.getPrimaryKey());
}
}
m_loader.addPersistenceMappingFixup(new ContextFixup(getHelper())
{
public void fixup()
{
Metaclass type = (Metaclass)attribute.getType();
PersistenceMapping mapping = type.getPersistenceMapping();
if (mapping == null)
{
throw new MetadataException("err.meta.missingAssocPersistenceMapping",
new Object[]{attribute.getName(), metaclass.getName(), type.getName()});
}
classMapping.setDestinationKey(mapping.addForeignKey(sDestinationKeyName, classMapping));
}
});
}
mapping = classMapping;
}
relationalMapping.addAttributeMapping(mapping);
}
});
}
});
XMLUtil.withFirstChildElement(relationalMappingElement, "Hook", false, new ElementHandler()
{
public void handleElement(Element componentElement)
{
final Component hook = new Component("<PersistenceHook:" + metaclass.getName() + ">");