Hibernate version:3.1.3
Hi -
I have a little trouble with persisting objects.
I have an interceptor in which I want, based on the state of the object passed in onFlushDirty, to change the state of an associated object.
Code:
public void onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyName, Type[] type, SecurityUser secUser) throws CallbackException
{
// Change variable
boolean changed = false;
// Surgery
if(entity instanceof Surgery)
{
Surgery surgery = (Surgery) entity;
// Extract values
String oldState = null, newState = null;
int state = -1;
SurgeryRoom surgeryRoom = null;
for(int i = 0; i < currentState.length; i++)
{
String property = propertyName[i];
// State
if(property == "state")
{
oldState = (String) previousState[i];
newState = (String) currentState[i];
state = i;
continue;
}
// Surgery
else if(property == "surgeryRoom")
{
surgeryRoom = (SurgeryRoom) previousState[i];
}
}
// Checking changes in state
if(!newState.equals(oldState))
{
if(newState.equals("Ongoing"))
{
logger.info("starting Surgery " + surgery.getId());
if(surgeryRoom != null)
{
if(surgeryRoom.getOngoingSurgery() != null)
{
if(surgeryRoom.getOngoingSurgery() != surgery)
{
throw new CallbackException("Cannot start Surgery:[Id=" + id + "] because there is an ongoing Surgery:[Id=" + surgeryRoom.getOngoingSurgery().getId()+"] in SurgeryRoom:[Id=" + surgeryRoom.getId() + "]!");
}
}
else
{
surgeryRoom.setOngoingSurgery(surgery);
}
}
changed = true;
}
else if(newState.equals("NotBegun"))
{
logger.info("aborting Surgery " + surgery.getId());
if(surgeryRoom != null)
{
if(surgeryRoom.getOngoingSurgery() != null)
{
if(surgeryRoom.getOngoingSurgery().getId() == surgery.getId())
{
surgeryRoom.setOngoingSurgery(null);
}
}
}
changed = true;
}
else if(newState.equals("Finished"))
{
logger.info("finishing Surgery " + surgery.getId());
if(surgeryRoom != null)
{
if(surgeryRoom.getOngoingSurgery() != null)
{
// We have to check on id as we are dealing with copies.
if(surgeryRoom.getOngoingSurgery().getId() == surgery.getId())
{
surgeryRoom.setOngoingSurgery(null);
}
}
}
changed = true;
}
else if(newState.equals("Cancelled"))
{
logger.info("cancelling Surgery " + surgery.getId());
if(surgeryRoom != null)
{
if(surgeryRoom.getOngoingSurgery() != null)
{
if(surgeryRoom.getOngoingSurgery().getId() == surgery.getId())
{
surgeryRoom.setOngoingSurgery(null);
}
}
}
}
}
}
return changed;
}
However sometime the SurgeryRoom object get persisted and sometimes not.
I know that persisting of objects is managed by findDirty. Is there any possibility for me to mark the SurgeryRoom object dirty and force a onFlushDirty on it?
Perhaps my approach is wrong altogether and I should have chosen a different strategy?
Any help is appreciated.