-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: dynamic association fetching
PostPosted: Mon Feb 09, 2004 2:29 am 
Newbie

Joined: Thu Jan 29, 2004 3:52 pm
Posts: 16
Hi,

Criteria.setFetchMode(String associationPath,FetchMode mode) API specifies that associationPath parameter is "a dot seperated property path". Does this imply that we can navigate relationships to specify fine grained object fetching strategy at runtime?

If yes, how would one specify a property path for Product given example of Customer 1--N Order 1--N Product? Is it simply:

Code:
List prods = sess.createCriteria(Customer.class)
    .add( Expression.eq("name", "king") )
    .setFetchMode("orders", FetchMode.EAGER)
    .setFetchMode("orders.product", FetchMode.LAZY)
    .list();


or we have to somehow chain another Order Criteria to the first one where we specify fetching strategy for the product?

Regards,
Vladimir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 6:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Have you tried it?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:23 am 
Newbie

Joined: Thu Jan 29, 2004 3:52 pm
Posts: 16
gloeglm wrote:
Have you tried it?


No I haven't. This is my third day of using hibernate and things tend to get overwhelming. I am still checkmarking all the needed features and I have to admit the list is long and getting shorter slowly. I tried to checkmark this one without getting my feet wet. However, that said I have already "contributed" to JIRA two new feature approved requests :)

Regards,
Vladimir


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 9:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
vlada wrote:
that said I have already "contributed" to JIRA two new feature approved requests :)

If I ask for JIRA updates, it doesn't mean it will be accepted. That's because I think it is worth keeping an eye on it however :)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 10:32 am 
Newbie

Joined: Thu Jan 29, 2004 3:52 pm
Posts: 16
gloeglm wrote:
Have you tried it?



Is it possible or not? If you tell me its possible, I'll try examples myself.

Cheers,
Vladimir


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 10:48 am 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
I have the same need, and I have tried it. It doesn't work.

Staying with your example: If I specify "orders", it will fetch the orders. But if I specify "orders.products", it doesn't fetch either one. If I specify both "orders" and "orders.products", it will fetch orders only.

So how does the association path in setFetchMode work then? Or is it a bug? The only examples I can find use a single property--not a path.

I'm using Hibernate 2.1.6.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 11:32 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
When you say you specify each property individually does that mean when you specify orders.product as LAZY in each of those cases?

If orders.product is lazy then if might be applying it to the whole chain. If that is the case (and if by design) then what you describe makes sense.

Of course it also depends on the mappings as well. Are the collections defined as lazy in the mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 11:49 am 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Oops. I didn't read the original example closely enough. My scenario might be a little different.

In my mapping file, all classes and associations are marked with lazy="true". I'm trying to use setFetchMode to override the mapping and eagerly fetch the children and the grandchildren of the root objects. The JavaDoc for setFetchMode says that the association string can be a dot-separated path, which implies I should be able to do something like this:

Code:
setFetchMode("orders.products", FetchMode.EAGER);


But in my test, this did not work--nothing was eagerly fetched. So how does the "dot-separated path" work?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 4:24 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I would read the docs on setFetchMode, it doesn't mean what you think (or I thought originally) it is.

setFetchMode to EAGER only means that it will do an outer join when fetching the objects.

LAZY simply means that it won't outer join not that it won't fetch them at all. It's a bit confusing and got me initially as well.

Which is probably why they deprecated both of them in Hibernate 3

public static final FetchMode LAZYDeprecated. use FetchMode.SELECT
Fetch lazily. Equivalent to outer-join="false".

public static final FetchMode EAGERDeprecated. use FetchMode.JOIN
Fetch eagerly, using an outer join. Equivalent to outer-join="true".

However, it would be nice to be able to specify lazyiness at query time in the Criteria. If there is one then I have missed it :D


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 5:30 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
VampBoy wrote:
However, it would be nice to be able to specify lazyiness at query time in the Criteria. If there is one then I have missed it :D


According to Hibernate In Action, this is exactly what FetchMode.EAGER is intended to do. (See page 260, section 7.3.2.) And it seems to work fine, as long as you pass in just a property name. But if you pass in a dot-separated path as the JavaDocs say you can, it doesn't seem to work at all.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 6:58 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Well, I don't know what to say but I actually tried this out and I think I found the same thing.

I tried to set the fetch mode to eager on a many-to-one + "." + one-to-many and I got a init exception when trying to check the size.

I tried the same thing where I loaded up the many side and then specified a EAGER fetch mode and was able to call size() on it with no issue.

This blew a lazy init exception.
Code:
    public void test() throws Exception {
        Session session = HibernateConfigurationHelper.getSession();

        Criteria criteria = session.createCriteria(Analysis.class);

        criteria = criteria.setFetchMode(Analysis.NOTES_PROPERTY + "." + Note.NOTE_DETAILS_PROPERTY, FetchMode.JOIN);

        List results = criteria.list();
        session.close();

        for (Iterator it = results.iterator(); it.hasNext();) {
            Analysis analysis = (Analysis) it.next();
            System.out.println("analysis = " + analysis.getAnalysisId());
            if (analysis.getNotes() != null) {
                System.out.println(analysis.getNotes().getNoteDetails().size());
            }
        }
    }



This did not.
Code:
    public void test2() throws Exception {
        Session session = HibernateConfigurationHelper.getSession();

        Criteria criteria = session.createCriteria(Notes.class);

        criteria = criteria.setFetchMode(Note.NOTE_DETAILS_PROPERTY, FetchMode.JOIN);

        List results = criteria.list();
        session.close();

        for (Iterator it = results.iterator(); it.hasNext();) {
            Note note = (Note) it.next();
            System.out.println("note = " + note.getNotesId());
            System.out.println(note.getNoteDetails().size());
        }
    }


Don't know what to say but the java doc still implies that its main use is to specify outer joinability at run time.

"Specify an association fetching strategy for a one-to-many, many-to-one or one-to-one association, or for a collection of values. "


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 8:51 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Well, it's nice to know someone else can reproduce the problem. So can someone from the Hibernate team clarify how the association path is supposed to work in setFetchMode? Or confirm that this is a bug?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 8:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No it doesn't work at present (I only noticed this three days ago), yes its on my list to fix.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 10:23 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
As a side note gavin, is there any thoughts on maybe doing up something like this for true lazy loading specification?

i.e. I believe you can only set the EAGER fetch mode for one collection.

We have a rather beefy object model with all of our collections as lazy loading. We end up walking the tree if we need to initialize something to return to the client. It would be nice to have a mechanism to say "I am loading X and I want the W, Y, Zs on it as well.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 08, 2005 10:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You want FecthMode.IMMEDIATE_SELECT?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.