-->
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.  [ 7 posts ] 
Author Message
 Post subject: Force eager load of entire graph?
PostPosted: Mon Oct 18, 2004 10:29 am 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
Hibernate version:2.1.6

Is there a way to run a query and force everything to be loaded in the object graph?

We are working with relatively small datasets here, and based on the last 4 years of data, we will continue to do so.

Retrieving the entire object graph on a query and sending it back to the web tier makes development much simpler - wrapping everything in a DTO doesnt buy us anything, and would force multiple SQL queries back to the DB when we mapped the model objects to the DTO.

We are hitting the "no session available" exception on the client since we have serialized and returned a lazily loaded object/collection/mapping back to the client.

Lazy loading speeds up our daily import process significantly so Id like to keep it enabled, but Id like to disable it for certain initial user-selection queries.

Is there a flag I can set to get Hibernate to run a particular Query and fetch everything? Ive looked at the Critieria API, but this seems to require me to set up eager loading for all properties one at a time - Im effectively replicating the mapping model here, and according to the book, the Critiera API will only eagerly fetch a single collection.

Nick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 11:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
it depends on your graph of object.

Fetch keyword in hql can help.

You have limitation with to-many association.

All this is explained in the reference guide and very well explained in hibernate in action

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 11:38 am 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
Yes, Ive got (and read) both. Hibernate seems brilliant for 2T, or server side only processing.

This is what prompted my question - having all the data loaded on the server is great, but I need to display/manipulate it on the GUI tier - now Im back to making DTO copies of my domain objects in order to transport them back to the client. Granted, I could disable lazy loading, but then the daily data load suffers massively.

However, the benefits of using Hibernate with web apps seem to drop away fairly rapidly when using Hibernate on the middle tier with a GUI (Swing or Web) to manipulate that data.

Is there anything in 3.x that is aimed at making life easier in this scenario?


Nick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 12:19 pm 
Regular
Regular

Joined: Fri Sep 03, 2004 2:01 pm
Posts: 51
Can I invert this?

Could I disable lazy loading by default, and then enable it selectively for certain queries?

Nick


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 18, 2004 12:35 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
In addition, a "fetch" join allows associations or collections of values to be initialized along with their parent objects, using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections.


http://www.hibernate.org/hib_docs/refer ... ryhql.html

this works perfectly if you have lazy = true in mapping file and want to force fetching in hql but i don't know if the inverse works...

can you just show how the graph is i.e

Code:
A 1-- * B
|
|
C1--1D


(use code tag to do it)

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 5:30 am 
Newbie

Joined: Thu Oct 06, 2005 5:36 am
Posts: 8
Nick,

We are hitting on the same issue as the one you described. Did you find a solution for this problem (loading sometimes the full graph / sometimes pieces of the graph (GUI) )

Cheers,
David


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 24, 2006 7:00 am 
Senior
Senior

Joined: Tue Mar 09, 2004 2:38 pm
Posts: 141
Location: Lowell, MA USA
If your object has only one-to-one or many-to-one realtionships, this approach works great. But how many one-to-many and/or many-to-many realtionships are in the persistent class you're trying to pass through to the client tier? If you have more than one, a fetch join is going to hinder performance. What Hibernate 3 ( I notice you're still using 2.16) does is joins the tables you want to eagerly load. So if you have an Order and what to fetch join a Set of assocated Items, you'll get back a SQL result set that is equal to the size of the number of Items. Each row will contain the data from both the ORDER and ITEM tables. If Orders have more than a single one-to-many association, the result set would be a nasty cartesian product. You should avoid using multiple fetch="join" on multiple "-to-many" relationships.

If you used an HQL Query, you will have to "narrow" the result set by passing the result of the Query to a Set so that you will get back a distinct group of Order instances with thier items initialzied. Otherwise, if there are 2 Orders in your resultset each with 20 orders, you will get back 40 Order instances in the List. Passing the result to a Set, you can narrow it so that you end up with 2 Orders each with 20 Items. In Hibernate 3.x, the Criteria API handles this a bit better and allows you to use a Result Transformer which will do this for you. Hibernate 3.2 allows you to ResultTransformers with HQL queries too.

For situations like this, I have found the Criteria API to be a bit more flexible in this area. However, the guts of the API has been significantly improved in the 3.x series. I'd highly recommend you upgrade if you can. The migration is realatively painless.

Ryan-

_________________
Ryan J. McDonough
http://damnhandy.com

Please remember to rate!


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