-->
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: How to use sql-select for this query?
PostPosted: Thu Apr 09, 2009 1:56 pm 
Newbie

Joined: Tue Jun 03, 2008 5:29 pm
Posts: 3
I have a tree-explorer java widget tied to hibernate. To correctly display whether a node has children (so as to display the '+' or not), I would like to use hibernate to find if the node has children, but not return all the children.

Can i use <sql-select> for this? If I use select count(*), is there a way to map this to a boolean if the result is > 0? Or do I have to do this programmatically?

Is there a better way to do this?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 10, 2009 9:05 am 
Newbie

Joined: Mon Sep 18, 2006 11:15 am
Posts: 6
The usual way I've done this is as you describe: do a select count(*) and then convert to boolean in code doing a > 0.

e.g.
Code:
boolean booleanResult = ( (Long) session.createQuery("select count(*) from ....").iterate().next() ).longValue() > 0;


BTW I don't know what is meant by <sql-select>: is this something specific to your programming environment?

Using a HQL query it is possible to construct an object in the select clause.

http://www.hibernate.org/hib_docs/v3/re ... hql-select
- has more details on what could go in the select clause.
So you could have a custom java class that takes the result of the count(*) and coverts it to boolean.
e.g.

Code:
public class CountInfo
{
   public final boolean result;
   public CountInfo(long count)
   {
       result = count > 0;
    }
}

then in the Hibernate query:
Code:
Query query = session.createQuery("select new CountInfo(count(*))
from ..."); // TODO: fill in rest of HQL query
List<CountInfo> result = (List<CountInfo>)query.list();
boolean booleanResult = result.iterator().next().result;


However, I don't think this is a better that doing.

So - to answer your question: No I don't think there is a better way to do this.
At least I can't think of one!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 10, 2009 3:08 pm 
Newbie

Joined: Tue Jun 03, 2008 5:29 pm
Posts: 3
Hi and thanks for the response! That helps quite a bit.

The <sql-query> thing is apparently a way to embed actual sql statements in your hbm files.

Not sure why I would want to put them there as opposed to what you suggested...

Cheers


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.