I have a object graph which is defined by hibernate
node -> node ( many-to-many )
I have set the children with update cascade behaviour. The hibernate did the job prefectly. I can update a node and its auto update any change in its children.
But there is problem when I build a UI ( Java standalone client by SWT ) , call EJB and EJB call hiberante.
The problem is when hibernate update the node ( which is loaded from hibernate) , it will actully make some change on the object itself. In normal case, java 's object is pass-by-reference, that's fine. I use the updated node and do other things. Yet, during EJB calls are pass-by-value. The updated object cannot be passed back to the client whiich lead to problem.
An Example ( copy-and-paste button mechanism )
1) UI load the NodeA from EJB/hibernate
2) Press copy button, a reference is point one of the child in NodeA
3) Press paste button, the reference is added to somwhere
4) Press save, call EJB and hibernate save the record
( Up to above working fine )
5) Press paste again, the reference is added to somewhere ( currently the UI holding the node which passed to EJB, since EJB cannot pass-by-reference, its state does not change. ie. the old one )
6) Press save, call EJB and hibernate save the record. This will generally getting error ( either update incorrectly or generating error message.
Simple Test programme reproduced the problems
Code:
List a = bean.getNodeList(); // load the list from a EJB
Node r1 = (Node) a.get(0); // get the first node
Node r2 = (Node) a.get(1); // get the second node
r1.addNode(r2); // add one more link
bean.updateNode(r1); // work fine
r1.addNode(r2); // add second link
// error. this is because r1.addNode(r2) using non-updated r1 to do further change. Error occur
bean.updateNode(r1);
The error could be an exception or incorrect update. I would like to know is there better way to solve this ?