{
JPOXLogger.PERSISTENCE.debug(LOCALISER.msg("052217",
StringUtils.toJVMIDString(sm.getObject()), fmd.getFullFieldName()));
}
RDBMSManager storeMgr = (RDBMSManager)sm.getStoreManager();
ObjectManager om = sm.getObjectManager();
ClassLoaderResolver clr = om.getClassLoaderResolver();
AbstractMemberMetaData[] relatedMmds = fmd.getRelatedMemberMetaData(clr);
// TODO Cater for more than 1 related field
String fullClassName = ((AbstractClassMetaData)relatedMmds[0].getParent()).getFullClassName();
//TODO I'm not sure that we need to loop all implementations. will we have the fk set to all tables, if many?
String[] classes;
if (((AbstractClassMetaData)relatedMmds[0].getParent()) instanceof InterfaceMetaData)
{
classes = storeMgr.getOMFContext().getMetaDataManager().getClassesImplementingInterface(fullClassName, clr);
}
else
{
classes = new String[] {fullClassName};
}
Set datastoreClasses = new HashSet();
for (int i=0; i<classes.length; i++)
{
// just remove duplicates
datastoreClasses.add(storeMgr.getDatastoreClass(classes[i], clr));
}
Iterator it = datastoreClasses.iterator();
while (it.hasNext())
{
DatastoreClass refTable = (DatastoreClass)it.next();
JavaTypeMapping refMapping = refTable.getFieldMapping(fmd.getMappedBy());
if (refMapping.isNullable()) // Only clear the references that can be cleared
{
// Create a statement to clear the link from the previous related object
StringBuffer clearLinkStmt = new StringBuffer("UPDATE " + refTable.toString() + " SET ");
for (int j=0;j<refMapping.getNumberOfDatastoreFields();j++)
{
if (j > 0)
{
clearLinkStmt.append(",");
}
clearLinkStmt.append(refMapping.getDataStoreMapping(j).getDatastoreField().getIdentifier());
clearLinkStmt.append("=NULL");
}
clearLinkStmt.append(" WHERE ");
for (int j=0;j<refMapping.getNumberOfDatastoreFields();j++)
{
if (j > 0)
{
clearLinkStmt.append(" AND ");
}
clearLinkStmt.append(refMapping.getDataStoreMapping(j).getDatastoreField().getIdentifier());
clearLinkStmt.append("=?");
}
try
{
ManagedConnection mconn = storeMgr.getConnection(om);
SQLController sqlControl = storeMgr.getSQLController();
try
{
// Null out the relationship to the object being deleted.
PreparedStatement ps = null;
try