-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I eagerly load a ref to a 3rd object in a collection?
PostPosted: Thu Feb 01, 2007 1:26 am 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I have a query where I have three objects, and I want to load all three objects eagerly. I've choosen to mark all objects lazy in the annotations just as recommended. So here are my objects:

Device ----> NIC ----> DNS Name

Where Device has many NICs and a NIC has many to one DNS Names.

I can do a query to eagerly load the nics, but I don't know how to specify to load the DNS Names that go with the NICs as well.

from Device d left join fetch d.nics left fetch join d.nics.dns

Unfortunately the above query doesn't work because nics is a set and not a NIC instance. How do I specify that I want join the NIC instances with their DNS Name?

As of right now I see the DNS names being loaded one by one resulting in the N+1 query problem.

Thanks
Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 4:01 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
Why don't you leave everything LAZY and query at the NIC level instead of pulling from the top-down?

This is pretty common w/ large hierarchies. You could pull NIC and then left join fetch to Device & DNS Name.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 6:13 pm 
Beginner
Beginner

Joined: Tue Nov 29, 2005 4:42 pm
Posts: 49
Location: Atlanta, GA
I hadn't thought about that. That's pretty creative. I'm not positive that would work since my view wants the devices displayed along with their NICs and DNS. I can see if it works or not.

So is this to say I can't do this type of deep query joins? I am just curious if it's possible. This has got to be a common thing as you said with large hierarchies. It would only make sense that Hibernate/EJB provide assistance in this matter. You can do it with SQL pretty easy at least.

Charlie


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 6:51 pm 
Regular
Regular

Joined: Mon Nov 14, 2005 7:33 pm
Posts: 73
chubbard wrote:
I hadn't thought about that. That's pretty creative. I'm not positive that would work since my view wants the devices displayed along with their NICs and DNS. I can see if it works or not.

So is this to say I can't do this type of deep query joins? I am just curious if it's possible. This has got to be a common thing as you said with large hierarchies. It would only make sense that Hibernate/EJB provide assistance in this matter. You can do it with SQL pretty easy at least.

Charlie


I'm pretty sure (though I haven't read to confirm) that the JPA spec doesn't support joins at that level, though Hibernate *can* handle it, it only seems to work if you're not chaining out through a collection.

Again, don't take my word for it...I'm just recalling from memory...I could be wrong.

I know this query works for me but I'm joining 2 levels deeper through single entities:

Code:
     String query = "select otd from OrderTrafficDetail otd " +
        "left join fetch otd.orderTraffic " +
        "left join fetch otd.customerOrder " +
        "left join fetch otd.customerOrder.deliveryRegion " + //bingo!
        "left join fetch otd.orderTraffic.vendor " +
        "where otd.orderTraffic.shipDate = :shipDate " +
        "order by otd.orderTraffic.carrierNo";


Anyhow, w/ some fenagling (sp?) you should be able to just approach the model from the middle and get the same data....I do the same trick when I want the app to perform a little better.


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