-->
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: Fetching Hierarchy
PostPosted: Thu Dec 10, 2009 11:18 am 
Newbie

Joined: Thu Dec 03, 2009 1:37 pm
Posts: 7
Hi,

I want to fetch data in hierarchical order from database using HQL.

I have a table with self reference

Item
------------
Item_Id
Item_Name
Parent_Item_Id

Class Item
{
int Id;
string Name;
List<Item> Children;
}

Now I want to fetch all the data till the last child.
If I run the query

from Item item join fetch item.Children

Then it will fetch all the children of first level items not the whole hierarchy. I want to fetch whole hierarchy in single hql query.

Can anyone please help?

Thanks in advance :)

Regards,
Niranjan


Top
 Profile  
 
 Post subject: Re: Fetching Hierarchy
PostPosted: Mon Jan 18, 2010 3:19 pm 
Newbie

Joined: Mon Jan 18, 2010 3:08 pm
Posts: 2
Hi Niranjan,

This would be translated into a 'connect by' query to the underlying database. Unfortunately, Hibernate doesn't support this as of yet.

Only options are to work with views (abstract the recursive connection) but this may not always work as you'd need to know how 'deep' the recursion goes beforehand or to programmatically iterate through and dive into the result sets. I.e. select individual records and use it's content to fire a new query (to select it's child of parent). This will not be very efficient, tough.

Regards,

Arjan


Top
 Profile  
 
 Post subject: Re: Fetching Hierarchy
PostPosted: Wed Jan 20, 2010 6:19 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Do you need to be able to read selected "subtrees" (e.g. one item and all of its subitems) ? In that case you probably need a SQL Query. If you only need to read the complete tree, it's easier because you can simply load the complete table and hibernate creates the hierarchy for you (at least its working in my case).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Fetching Hierarchy
PostPosted: Tue Mar 23, 2010 8:51 am 
Newbie

Joined: Thu Dec 03, 2009 1:37 pm
Posts: 7
Hi Wolli,

In my project, to support multiple databases I have used NHibernate. As you have suggested to use Sql query then the purpose is gone. So I need something from Nhibernate such as "Connect by" clause in oracle and "With statement" in sql server.

That will be more beneficial for me.

Thanks,
Niranjan


Top
 Profile  
 
 Post subject: Re: Fetching Hierarchy
PostPosted: Tue Mar 23, 2010 11:11 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
NHibernate doesn't support that. Afaik you can write custom functions and there corresponding sql for each dialect, but I've never done that and can't give you much information about that.

Something like that:

http://stackoverflow.com/questions/1845884/custom-sql-function-for-nhibernate-dialect

_________________
--Wolfgang


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.