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.  [ 8 posts ] 
Author Message
 Post subject: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Nov 24, 2009 4:38 am 
Newbie

Joined: Sun Nov 22, 2009 6:42 pm
Posts: 7
Hi,

Suppose you have a Car that has a set of many Parts, mapped as:

Code:
(in Car's mapping)

<set name="parts">
  <key column="car_id" not-null="true" />
  <one-to-many class="Part" />
</set>


Now, say you want to build a servlet that queries for Cars and returns them, and the client wants to display the Car and all its Parts. How do you do this? My call to the servlet keeps failing..

I tried adding 'lazy="false"' to the <set> but to no avail. In the Eclipse debugger, I'm seeing that the Hibernate-generated Car contains a PersistentSet as its set... Is that not serializable? If I copy all the elements from that PersistentSet into a HashSet and set the HashSet as the Car's parts, it works... But this is just nasty..

Beyond that, having a lazy=false is non-ideal. Sometimes I want to return info to the client, so I want it to initialize everything, while sometimes I'm just dealing with partial data within the server, in which case I'm happy to let it be lazy. Is there a way to tell Hibernate which queries you want to have executed lazily on the fly?

BTW, I'm using GWT, so my client is also written in Java.. The client and server share the same POJOs. The error I'm seeing says "The call failed on the server; see server log for details", but unfortunately I cannot find anything in the server logs (???). [I'm also new to GWT.. sigh]

To recap, my questions are:

1. Is PersistentSet not serializable?
2. How can I deal with this issue, beside adding all the elements of the PersistentSet to a HashSet and returning that?
3. Is there a way to programmatically tell Hibernate which queries I want it to perform lazily?

Thanks in advance, and sorry for my n00bness


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Nov 24, 2009 5:09 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You can keep your mapping as lazy and use a HQL-query with "join fetch" or critieria api with setFetchMode everytime you need other fetch strategies.

Your code with copying the set is probably working because when you copy the set the session is still open so there is no lazyInitializationException.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Nov 24, 2009 6:04 am 
Newbie

Joined: Sun Nov 22, 2009 6:42 pm
Posts: 7
mmerder wrote:
You can keep your mapping as lazy and use a HQL-query with "join fetch" or critieria api with setFetchMode everytime you need other fetch strategies.

Your code with copying the set is probably working because when you copy the set the session is still open so there is no lazyInitializationException.


Cool, thanks! That answers question 3.

Indeed, I had to leave the session open to avoid the exception when I was not setting "lazy="false"". However, regardless of whether I do "lazy="false"" OR leave the session open and copy objects with "lazy="true"", if I don't replace that PersistentSet with a HashSet, my RPC fails. So something in PersistentSet is messing up the RPC... Thus, questions 1-2 are still open.

My guess is that even if PersistentSet is Serializable (question #1), it seems that GWT requires anything that needs to be sent over an RPC to implement an IsSerializable interface. I'm pretty sure PersistentSet does not implement IsSerializable... So I have a 4th question:

4. Is there a way to tell Hibernate which Set implementation to use when it generates a class that requires a set? I just want to build a wrapper for PersistentSet that implements IsSerializable, so I can use it with GWT.


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Nov 24, 2009 6:17 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Sorry, no idea about that gwt issue. but normally that should not be necessary. Just out of curiosity: instead of copying the set, just call getSize()-Method on it (so that it gets initialized) and tell me if that works.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Nov 24, 2009 10:24 pm 
Newbie

Joined: Sun Nov 22, 2009 6:42 pm
Posts: 7
Hi,

Well, it seems that PersistentSet may be Serializble, but GWT just doesn't support it.. Other sites that talk about Hibernate+GWT recommend that I do exactly what I'm doing: copy the set elements into a Set implementation that's supported by GWT (like HashSet). Same with List and PersistentList.

And to answer your question: nope, calling getSize() doesn't help because even though it's initialized, the instance of the Set is an instance of PersistentSet, so it still fails.

Just out of curiosity, I'd still like to know the answer to question 4. I'm pretty sure the answer is "yes", but I wanna know how..

How do I tell Hibernate which List/Set implementation to use when it generates a class that requires it?


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Wed Nov 25, 2009 4:48 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I don't think it is possible to tell hibernate not to use a persistent set. This is necessary to do all the smart collection handling hibernate does, like lazy loading, one shot delete and so on.

Correct me if I am wrong.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Dec 01, 2009 2:32 am 
Newbie

Joined: Sun Nov 22, 2009 6:42 pm
Posts: 7
Sure, but I thought maybe PersistentSet implements some HibernateCompliantSet interface that I can also implement with MyFunkySet, and then ask Hibernate to use MyFunkySet instead.. Since MyFunkySet is a HibernateCompliantSet, Hibernate shouldn't care..

But oh well, maybe that's not the case.

Thanks for your help!


Top
 Profile  
 
 Post subject: Re: Lazy evaluation across a servlet, more specifically with GWT
PostPosted: Tue Dec 01, 2009 5:50 am 
Newbie

Joined: Mon Nov 30, 2009 10:31 am
Posts: 2
you might want to take a look at the gilead (original name hibernate4gwt) project... works quite well, albeit it can be a little hard to setup...


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