-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernated DAOs and serialization: best practices?
PostPosted: Wed Jan 21, 2004 10:50 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
What is the interaction between Hibernate classes and Java serialization?

We would like to write (for instance) session beans with interfaces such as:

public List getClients();

which would return a List of Client objects fetched on the server with

session.find("from Client");

Eventually, each Client object will have a lazy list of Campaign objects (a standard lazy parent-child mapping where Client is the parent and Campaign is the child).

What might happen if we try to return a List of such Client objects from a session bean, and the session bean is remote? Could we get RemoteExceptions because the Hibernate lazy collection is not serializable?

Additionally, what if we want to pass Clients around in JMS MapMessages? Might we also have similar serialization problems?

What are the best practices with respect to passing Hibernated data objects around between (possibly remote, possibly message-driven) beans? Should we have "flattener" methods which handle our own fetching, to ensure we never pass lazy collections or proxies over the wire? (This is certainly doable, we just want to understand whether we are right to be thinking about this issue or whether there is some other approach altogether....)

Thanks very much.
Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 4:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Passing lazy collections around is no problem, they are all serializeable. Note however that you can't access them on the client side (no more session available to initialize)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 2:03 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
OK. So if I want to make sure that the client side will not hit any exceptions, then I should make sure that the whole collection has been fetched?

I just wanted to verify that I don't have to do something like:

class MyStatelessSessionBean {
public Client getFirstClient () {
// open Session...
Client client = (Client)(session.find("from Client client where client.id='foo'").get(0));
HashSet nonLazyCampaignSet = new HashSet();
nonLazyCampaignSet.addAll(client.getCampaigns());
session.close();
client.setCampaigns(nonLazyCampaignSet);
return client;
// handle exceptions...
}

In other words, I wanted to make sure that it's sufficient to be sure the lazy collection is fully fetched, rather than *also* having to make sure that the collection is serializable. Sounds like it will all be OK :-)

Also I've found the many other threads discussing issues of multi-tier laziness, object graph fetching (CarrierWave), etc., etc. Love this forum!

Thanks,
cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 5:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
It is sufficient to do this:

Code:
class MyStatelessSessionBean {
public Client getFirstClient () {
// open Session...
Client client = (Client)(session.find("from Client client where client.id='foo'").get(0));
Hibernate.initialize(client.getCampaigns());

If you really have detatched objects (e.g. sending the objects to a swing application) you have to ensure the client side code does not hit any uninitialized collections, otherwise you get an Exception. If you are still in the server environment (e.g. Servlets, JSP) you should take a look at the Open Session in View pattern in the Wiki Community area.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 7:15 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
Groovy. Thank you! I love Hibernate :-)
Cheers ---
Rob


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.