-->
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: How to fetch lazy properties of a lazy property
PostPosted: Tue Mar 21, 2006 1:22 am 
Newbie

Joined: Fri Apr 29, 2005 11:55 pm
Posts: 14
Location: China-Australia
The title maybe a bit miss leading but not sure how to describe my problem, especially English's not my first language.

I had three classes, FirmUser Firm and FormType

A FirmUser has a list of Firms and each Firm has a list of FormTypes
Code:
public class FirmUser{
    private List<Firm> firms;
   
    // getter and setter
}

public class Firm{
    private List<FormType> formTypes;
   
    // getter and setter
}

public class FormType{
}


They have all been mapped in Hibernate, and fetching mode are all LAZY.

Here's how I retrieve a list of FirmUsers.

Code:
List results = session.createCriteria(FirmUser.class)
    .setFetchMode("firms", FetchMode.JOIN).list();


Now I'v got a list of FirmUser, the 'firms' property of each FirmUser has been initialised, but Hibernate did not fetch 'formTypes' for each firm, because the fetching mode is 'LAZY'

How do I get Hibernate to fetch 'formTypes' for each firm when I try retrieve a list of FirmUser?

Of course I can achieve this by setting the fetch mode to eager, it's not appropriate in my application


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 3:33 am 
Newbie

Joined: Thu Mar 16, 2006 5:43 am
Posts: 5
Location: Delhi, India
I have not tried the same but I can suggest you to use the static method provided by class Hibernate to achieve what you are looking for.

The method is ==> initialize(classInstance)

This method is used to poplulate all the lazy intialized properties of any class objects (may be in a list) with data fetched from the database. After intializing the object you can close the session and have access to all the information inside the fetched mapped class object.

In your case you can use
Hibernate.initialize(firmUser.getFirm());

I hope that solves the issue, if yer, please mark as solved...

_________________
Thanks and Regards,
Cpt. Blue Streak
_______________________________________


Top
 Profile  
 
 Post subject: Doesn't work
PostPosted: Tue Mar 21, 2006 6:30 pm 
Newbie

Joined: Fri Apr 29, 2005 11:55 pm
Posts: 14
Location: China-Australia
Sorry BlueStreak, it didn't work.

I think there must be some way to achieve that either in Criteria query or in HQL


Top
 Profile  
 
 Post subject: try this one for once
PostPosted: Wed Mar 22, 2006 2:55 am 
Newbie

Joined: Thu Mar 16, 2006 5:43 am
Posts: 5
Location: Delhi, India
Instead of using

List results = session.createCriteria(FirmUser.class)
.setFetchMode("firms", FetchMode.JOIN).list();

if you try


Criteria crit = session.createCriteria(FirmUser.class);
crit = crit.setFetchMode("firms", FetchMode.SELECT);
List results = crit.setFetchMode("firms.formTypes", FetchMode.SELECT).list();

it may help solve the issue as the hibernate API documents that
this option will instruct criteria to fetch eagerly, using a separate select.

_________________
Thanks and Regards,
Cpt. Blue Streak
_______________________________________


Top
 Profile  
 
 Post subject: Still not work
PostPosted: Wed Mar 22, 2006 3:00 am 
Newbie

Joined: Fri Apr 29, 2005 11:55 pm
Posts: 14
Location: China-Australia
hi BlueStreak, unfortunately, I tried 'select' fetch mode, still not working.


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.