-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: EJB pass-by-value hibernate object Problem
PostPosted: Sat Jan 10, 2004 1:15 pm 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
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 ?


Top
 Profile  
 
 Post subject: Re: EJB pass-by-value hibernate object Problem
PostPosted: Sat Jan 10, 2004 2:52 pm 
Pro
Pro

Joined: Mon Sep 08, 2003 4:30 pm
Posts: 203
catcat wrote:
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 ?


Make your bean return the saved/updated object.
i.e
r1 = bean.updateNode(r1)

--steve p.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 11, 2004 2:27 am 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
Quote:
Make your bean return the saved/updated object

This would not work in some case. For example, in the copy and paste example. The CopiedNode (r2) which has a reference to the "old" node child. Using
Code:
r1 = bean.updateNode(r1)
will let the reference of r1 up-to-date. but the CopiedNode (r2) still using the old reference. since update(r1) will updated r2 too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2004 10:25 pm 
Beginner
Beginner

Joined: Fri Jan 02, 2004 3:27 am
Posts: 23
Location: Hong Kong
Does anyone have the same problem ? Any suggestion for the design ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.