Request Factory Entity Relationships Lazy Loading

335 views
Skip to first unread message

PJ Herring

unread,
Nov 17, 2010, 7:02:29 PM11/17/10
to Google Web Toolkit
So I've been working on this problem with RequestFactory and Lazy
Loading. I am using a Service Layer to deal with operations on Domain
Objects. I ran into a problem when I was trying to "merge" an object
with my Hibernate Domain layer. Hibernate was throwing a "lazy
loading" error ("Failed to lazily initialize a collection"). The
problem was when I fetched an object via RequestFactory it did not
come with its relationships. So if I had a Donor with a collection of
Donations, the Donations do not come with the initial fetch unless
specified (i.e.
requestFactory.donorRequest().fetch(id).with("donations")). When I
removed the relationship from the Proxy Domain Object (so removing the
methods getDonations and setDonations from the DonorProxy), I did not
have this problem. The merge worked fine. Why is this the case?

Second, if this is the case it would seem beneficial to remove
relationships that you would normally want to lazy load from the
proxies and just make an explicit call to load up the collection. So
remove the getDonation and setDonation from the DonorProxy and when I
need those donations just make a call with the donorId to get them.
This seems counter intuitive to me, but seems also not like a terrible
idea. Basically I am trading of between having to make explicit calls
to load OneToMany relationships and being able to do merge's. AM I
CRAZY?

agi

unread,
Nov 18, 2010, 2:47:23 AM11/18/10
to Google Web Toolkit
Hi

I had this problem as well. I haven't found perfect solution.. What I
have done is while retrieving data from the database so e.g. in
findEntity(Long id) method I am forcing persistent layer to return my
collection immediately:
Entity findEntity(Long id)
{
// open transaction, get entityManager etc.
// ...


Entity obj = entityManager.find(id);

// force retrival of collection
obj.getDonations().size();

// handle transaction and entitymanager etc.
//...
}

I will be very glad to find out how to make it in a better way:)

Thomas Broyer

unread,
Nov 18, 2010, 6:39:21 AM11/18/10
to Google Web Toolkit

On 18 nov, 08:47, agi <agata.p...@gmail.com> wrote:
> Hi
>
> I had this problem as well. I haven't found perfect solution.. What I
> have done is while retrieving data from the database so e.g. in
> findEntity(Long id) method I am forcing persistent layer to return my
> collection immediately:
> Entity findEntity(Long id)
> {
>   // open transaction, get entityManager etc.
>   // ...
>
>   Entity obj = entityManager.find(id);
>
>   // force retrival of collection
>   obj.getDonations().size();
>
>   // handle transaction and entitymanager etc.
>   //...
>
> }
>
> I will be very glad to find out how to make it in a better way:)

I believe the idea is that you use a "request scoped" transaction/JPA-
session (or whatever, I'm not used to JPA/JDO and all that stuff).
See also http://code.google.com/p/google-web-toolkit/issues/detail?id=5389

PJ Herring

unread,
Nov 18, 2010, 12:35:51 PM11/18/10
to Google Web Toolkit
The problem with this is you will end up writing a lot of code to get
the whole object back. It makes a complex Data Access layer and makes
me wonder is it worth using a relational database. If I have to fetch
relationships separately (in either layer) why not use a key store
database.

koma

unread,
Nov 18, 2010, 2:02:20 PM11/18/10
to Google Web Toolkit
IMO, this problem is not really GWT related but standard hibernate/jpa
behaviour.
Either you define your relationships as lazy, which means that they
are not loaded when the parent object is loaded, or you define the
relations as eager fetch.
The best practice is lazy load and then load the needed relationships
on a adhoc basis using Hibernate.initialize(donor.getDonations()) to
fetch them.

So you want the donations to come with the donor, initialize that
relation during the transaction.
Outside the transaction, any relation that was not loaded will throw a
lazy load exception when you read any out of these related objects.

Reply all
Reply to author
Forward
0 new messages