-->
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.  [ 10 posts ] 
Author Message
 Post subject: In need of desperate help
PostPosted: Tue Feb 12, 2008 7:46 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
I have ~20 classes mapped in NHibernate
absolutely all classes are lazy = false, and at runtime
it makes millions of selects and very very slow
i've read Hibernate In Action and many documentation but
i couldn't find the best solution.

can somebody help?

i can't make anything lazy because i use all the data in the objects
to 'hydrate' other objects (business objects). of course,
i could make everything lazy but then i'd have to modify alot of
code to enable session connection/disconnection from
the client application, which is a very bad solution

all i want is to retrieve all association tree in the best time
without performing thousands of selects

thanks for help!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 12, 2008 10:41 am 
Beginner
Beginner

Joined: Tue Mar 27, 2007 4:54 am
Posts: 47
Use explicit queries and join the associated objects you want using SetFetchMode/Join (ICritiera/IQuery). Don't join too much tough, espacially if you have multiple collections on one entity (=cartesian product).

I'd suggest you try to use lazy loading. As said - if needed, create explicit queries matching your specific use case to ensure you have appropiate data when traversing your graphs.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 10:22 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
there isn't any way that i can optimally retrieve an object and
all the assosiacions?

Lets say we have
class Cat
{
public string Name {get;set;}
public List<Cat> Kittens {get;set;}
public List<Cat> Pals {get;set;}
}

If i'd want to retrieve all of the associations of a cat:
from Cat cat join fetch cat.Kittens, join fetch cat.Pals

This HQL generetes a single select if i'm not mistakeing.
But why do you say it makes a cartesian product?
That's inneficient.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 13, 2008 10:41 am 
Beginner
Beginner

Joined: Tue Mar 27, 2007 4:54 am
Posts: 47
Yes - it can be inefficient. That's what I wrote :)
It's really just how outer joins work. In your case the number of rows in the set would be [number of cats]*[number of kittens]*[numbers of pals].

If it performs too slow, you'll better see if it's more efficient to run two or more queries in your case instead.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 14, 2008 1:30 am 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
In most circumstances lazy loading makes sense, that's why in 1.2 it was changed to the default.

If your objects are going to passed through some type of boundary then I suggest using coarser grained objects. You can find an example here: http://elegantcode.com/2007/11/06/dtos-presentation-model-and-icriteria/ (I prefer ICriteria to IQuery). A single SQL statement will be used to populate the DTO.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 15, 2008 2:25 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Matrasinator wrote:
there isn't any way that i can optimally retrieve an object and
all the assosiacions?

Lets say we have
class Cat
{
public string Name {get;set;}
public List<Cat> Kittens {get;set;}
public List<Cat> Pals {get;set;}
}

If i'd want to retrieve all of the associations of a cat:
from Cat cat join fetch cat.Kittens, join fetch cat.Pals

This HQL generetes a single select if i'm not mistakeing.
But why do you say it makes a cartesian product?
That's inneficient.


It makes a cartesian product because it will return a row for every combination of Kitten and Pal.

For this case a MultiQuery can be used: see the end of this section: http://www.hibernate.org/hib_docs/nhibe ... ulti-query


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 6:31 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
Multiquery is efficient when retrieving all association for one object.
But when i want all objects and for each all collections?
I think there isn't any efficient way of obtaining this.

it's like i'd want to do something like this in SQL:

SELECT cat.* , ( SELECT * from Kittens WHERE cat_id = cat.Id ) ,
( SELECT * from Pals WHERE cat_id = cat.Id )
FROM Cats cat
which is imposiible i think


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 11:24 am 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
MultiQuery is an alternative. Fill up the session cache and then (implicitly) query that rather than the db. However as I indicated above I would use DTOs here.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 11:26 am 
Beginner
Beginner

Joined: Fri Aug 10, 2007 3:34 am
Posts: 44
i am using DTO's... i don't understand what you mean by DTO's though:
lightweight objects called Data Transfer Objects?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 11:41 am 
Beginner
Beginner

Joined: Fri Jan 12, 2007 1:08 am
Posts: 41
By DTOs I mean a targeted view of the Domain. The Domain is a more generalized model. DTOs have a more specialized purpose e.g. to populate a specific screen.

Note however that this thread suggests that NHibernate's use of proxies might require references to NHibenate on the remote side (which if it is true is not ideal): http://forum.hibernate.org/viewtopic.php?t=983775


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