* @param ownerSM StateManager of the Set
*/
public void clear(StateManager ownerSM)
{
boolean deleteElements = false;
ObjectManager om = ownerSM.getObjectManager();
if (ownerMemberMetaData.getCollection().isDependentElement())
{
// Elements are dependent and can't exist on their own, so delete them all
JPOXLogger.DATASTORE.debug(LOCALISER.msg("056034"));
deleteElements = true;
}
else
{
if (ownerMapping.isNullable())
{
// Field is not dependent, but is nullable so we null the FK
JPOXLogger.DATASTORE.debug(LOCALISER.msg("056036"));
deleteElements = false;
}
else
{
// Field is not dependent, and is not nullable so we just delete the elements
JPOXLogger.DATASTORE.debug(LOCALISER.msg("056035"));
deleteElements = true;
}
}
if (deleteElements)
{
// Find elements present in the datastore and delete them one-by-one
Iterator elementsIter = iterator(ownerSM);
if (elementsIter != null)
{
while (elementsIter.hasNext())
{
Object element = elementsIter.next();
if (om.getApiAdapter().isPersistable(element) && om.getApiAdapter().isDeleted(element))
{
// Element is waiting to be deleted so flush it (it has the FK)
StateManager elementSM = om.findStateManager(element);
elementSM.flush();
}
else
{
// Element not yet marked for deletion so go through the normal process
om.deleteObjectInternal(element);
}
}
}
}
else
{
int relationType = ownerMemberMetaData.getRelationType(clr);
if (relationType == Relation.ONE_TO_MANY_BI &&
om.getOMFContext().getPersistenceConfiguration().getBooleanProperty("org.jpox.manageRelationships"))
{
// Managed Relations : 1-N bidir so null the owner on the elements
om.getApiAdapter().isLoaded(ownerSM, ownerMemberMetaData.getAbsoluteFieldNumber()); // Make sure the field is loaded
Collection value = (Collection) ownerSM.provideField(ownerMemberMetaData.getAbsoluteFieldNumber());
Iterator elementsIter = null;
if (value != null && !value.isEmpty())
{
elementsIter = value.iterator();
}
else
{
// Maybe deleting the owner with optimistic transactions so the elements are no longer cached
elementsIter = iterator(ownerSM);
}
if (elementsIter != null)
{
while (elementsIter.hasNext())
{
Object element = elementsIter.next();
if (!om.getApiAdapter().isDeleted(element))
{
StateManager elementSM = om.findStateManager(element);
if (elementSM != null)
{
// Null the owner of the element
if (JPOXLogger.PERSISTENCE.isDebugEnabled())
{
JPOXLogger.PERSISTENCE.debug(
LOCALISER.msg("055010",
StringUtils.toJVMIDString(ownerSM.getObject()),
ownerMemberMetaData.getFullFieldName(),
StringUtils.toJVMIDString(element)));
}
elementSM.replaceField(getFieldNumberInElementForBidirectional(elementSM), null, true);
if (om.isFlushing())
{
// Make sure this change gets flushed
elementSM.flush();
}
}