Quote:
Because modules are independent from each other First module can delete parent object and all its children (due to cascade="all") and Second module can update some children of this parent BEFORE parent is removed
I think there is still something seriously wrong with the whole setup.
If your modules are anything like the things I typically call modules, they should define:
- what objects exists
- what you can do to/with these objects
They should not at all influence the meaning of a Unit of Work.
If I understand you correctly your processflow looks like this:
Modul-A-process
- manipulate some A objects
End-A-process
Modul-B-process (concurrent to the process before)
- manipulate some B objects
End-B-process
Persistence Process
- Start Session
- take manipulated Objects from the processes before
- merge them into the session
- commit
- end Session
End Persistence Process
Since the A and B process do not talk to each other they might do things that contradict each other, causing problem in the Persistence Process.
So I'd try to move to a different model, more like this:
Single Process
- open Session
- attach object to session (no matter if from Module A or B)
- manipulate object as desired
- repeat as necessary
- commit
- end Session
End Process
I currently can't imagine why two processes should manipulate stuff which is supposed to belong to one Unit-Of-Work, at least not in the way you describe it.
If you really have to merge your objects from two sources into one session, try the following:
- check if the object is persistent (i.e. has an id), if not it's new great, persist.
- if it is, check if it is actually still in the database (load or get). If it isn't it's deleted, throw it away
- otherwise merge the state present in the session with the one you where about to put into the session.
Would you mind telling a little more about the kind of system you are trying to build? Web/RCP, Why this strange setup of modules ... I'm curious about what I am missing.