-->
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.  [ 3 posts ] 
Author Message
 Post subject: SQL query with aggregate function
PostPosted: Tue May 23, 2006 3:26 am 
Beginner
Beginner

Joined: Tue Mar 28, 2006 3:44 am
Posts: 22
Hi all,

I read in the documentation that NHibernate is able to use some aggregate functions. But I have no clue how I can use them to fill a property of an DTO. Here's the problem:

We have some object tree structures, basically a kind of productgroup hirarchy. Now the guy who's in charge of this structure and the corresponding business processes came to me and gave me his "favorite" ;) SQL statement.
Code:
SELECT succ.outlet_tree_id, COUNT(*) AS level
FROM OUTLET_TREE AS root, OUTLET_TREE AS succ
WHERE  succ.lft BETWEEN root.lft AND root.rgt
and root.outlet_tree_id=1
group by succ.lft, succ.outlet_tree_name, succ.outlet_tree_id
order by succ.lft

As you can imagine behind outlet_tree_id the whole outlet_tree DTO is "hidden". In the where clause outlet_tree_id=1 has to be parameterized.

So far I thought about using a named query in the XML mapping file. But I have no clue how I can fill a property with the level value. There is no corresponding column in the database therefore no information about this property is in the XML mapping file.
Any hints on filling the whole DTO with the additional field for the level value will be very appreciated. :)

Thanks in advance
Chavez


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 12:21 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
actually, you already have your answer. go ahead and create the named query in your mapping file. then create a method in your DTO:

Code:
public int getLevel(int treeId) {
   session.getNamedQuery("myNamedQuery")
      .setInt32("outlet_tree_id", treeId)
      .List(); //.UniqueResult() ???
}


there is a bug in this code in which the call to the List() method will return an IList instead of an int. there is a way to return a .UniqueResult() but i don't remember the proper usage off the top of my head.

you could also try to make this an immutable property:

Code:
public int Level {
   get {
      session.getNamedQuery("myNamedQuery")
         .setInt32("outlet_tree_id", m_treeId)
         .List(); //.UniqueResult() ???
   }
}


but then you are relying on a private member variable m_treeId instead of a passed param. not sure if that works for you.

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 3:47 am 
Beginner
Beginner

Joined: Tue Mar 28, 2006 3:44 am
Posts: 22
Hi devonl,

thanks for your reply. :)
The statement returns the whole tree with the given ID as root. So the value of the level property changes if you choose another object as root. If I implement the level as immutable object, as you suggested, every object as to be asked for his level before a new tree can be build. The other guy isn't very comfortable with this approach. I can see his concerns too.

I played a bit with some stuff and came to a couple of ideas and questions. Is it possible to tell NHibernate it has to populate all DTO properties and the additional property for the level in the particular case of this named query? Until now I only saw that NHibernate returns a "list of lists" as result of a a prepared query. Prepared query in this case means a query generated through the CreateQuery() method. It looks like this:
Code:
[0] [0] value1
    [1] value2
    [2] value3
[1] [0] value1
    [1] value2
    [2] value3
[2] [0] value1
    [1] value2
    [2] value3

and so on...

To work with such a result I have to manually map every entry to a DTO or is there another way? Maybe I'm thinking in the wrong direction?!

Greetings,
Chavez


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