-->
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: What makes an object "detached"
PostPosted: Mon Mar 22, 2004 2:15 pm 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
I've searched the forums concerning layered architectures. I am currently using EJB/entity beans and DTOs to transfer data to the web tier. I would like to use something similiar using session EJB and hiberate. It appears I can make use of "detached objects" in hiberate but I havnt found any clear answer on when a hibernate object becomes detached so that it can be safely serialized (if need be) to the web tier. It may be the word "detached" is new terminology and I just need to change the keywords I am searching for.

If anyone could shed some light on how to use detached objects in hiberate, I would appreciate it. I have seen the ppt presentation which mentioned detached objects but it wasnt clear where the "detached" part came into play.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 22, 2004 9:02 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
I think "detached" means that the object is no longer associated with an active transaction. So if the object is changed in your code, the change will not get propogated to the DB. In this case, an object would be detached when the transaction is committed or the session is closed.

In regards to when to safely serialize the object for transport to the web tier, I think the answer is "anytime you want" because object serialization is read-only...it does not change the object contents. So, you can take an "attached" object and serialize that to the web tier without fear that your changing it in the DB.

I think using hibernate in a session bean is a great idea.

Chrisjan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 10:13 am 
Newbie

Joined: Mon Mar 22, 2004 2:06 pm
Posts: 9
Location: USA
Ok, thanks, that makes sense. When I said "safely serialized", I meant that any changes to the objects in the web tier would not result in the web tier trying to make database update or select calls.

So for example:

Session session = sf.openSession();
Transaction tx = session.beginTransaction();
AuctionItem item = (AuctionItem) session.get(ActionItem.class, itemId);

tx.commit(); // <- at this point Item is effectively "detached "
session.close(); // so any set calls on item after this point are not persisted
// until you do the following
...

Session session = sf.openSession();
Transaction tx = session.beginTransaction();
session.update(item);
tx.commit();
session.close();


And if you allow the EJB to handle transactions instead, then an object becomes detached as soon as the EJB method call completes and the object is returned to the caller.

If this is the case, then I just may use the hiberate objects as my value objects (DTOs) for the web tier. I am sure there are arguments that this could couple the data domain too closely to the web tier, but in my experience you typically have to change both anyway, and the design of the hibernate objects could reflect a less data centric view of the data as well.

Tim


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 23, 2004 2:25 pm 
Beginner
Beginner

Joined: Wed Mar 17, 2004 4:13 pm
Posts: 21
Location: San Diego, CA
I think everything you said is exactly right.

I understand now your concerns regarding the web tier making database updates. With ejb calls, it'd be fairly difficult for the web tier to have a database effect on a returned hibernate object. Ejb calls are typically RMI calls. So, the object returned is actually a new object. It is a serialized version of the original object, and since you have a new object, it is no longer tied to the orignal transaction. This could be app server dependent though. If you're doing this all on the same machine, an app server may optimize and give you back the original object.

Anywho, you're on the right track.

BTW, I use my data objects in the web tier too. I don't think there's anything wrong with doing so. The important thing is that you keep the db code which works on these objects in a data layer separate from the web tier - like you are planning to do in your session beans.


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.