-->
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: How to retrieve not a domain bean but some of its properties
PostPosted: Wed Jan 05, 2011 10:16 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hi,

I'm trying to retrieve some properties of a domain bean.

I have an HQL query with a projection as in:

Code:
   public List findCountByBrowser() {
      Query query = getSession().createQuery("select count(*) as count, visitorBrowser from StatisticsVisit group by visitorBrowser");
      return query.list();
   }


Is the List the appropriate type for it ? The compiler would like me to add a SuppressWarning directive as it lacks generic types.

How to iterate the results ? Do I need a convenience data bean with these two properties count and visitorBrowser ?

Or is there any better way to go..?

Thanks !


Last edited by stephaneeybert on Sun Apr 10, 2011 2:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: HQL query with projections
PostPosted: Sun Jan 23, 2011 5:43 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Anyone ?


Top
 Profile  
 
 Post subject: Re: How to retrieve not a domain bean but some of its properties
PostPosted: Thu Apr 14, 2011 5:27 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
No one ?

Is my issue not properly laid out or hard to understand ?


Top
 Profile  
 
 Post subject: Re: How to retrieve not a domain bean but some of its properties
PostPosted: Thu Apr 14, 2011 8:43 am 
Newbie

Joined: Tue Sep 14, 2010 4:29 pm
Posts: 16
Hi stephaneeybert,

in fact this is not well documented (or I haven't found the right spot yet). Judging from the behavior of SQL Scalar Queries described here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html#d0e17378 I'd guess that the items in the result list are of type Object[]. Provided I am not wrong, I'd write your method as follows:
Code:
   public List<Object[]> findCountByBrowser() {
      Query query = getSession().createQuery("select count(*) as count, visitorBrowser from StatisticsVisit group by visitorBrowser");
      // We will have to do an unchecked cast here to bridge from non generic list()-method to a generic return type. We
      // can suppress warnings with annotation.
      @SuppressWarnings("unchecked")
      final List<Object[]> resultList = (List<Object[]) query.list();
      return resultList;
   }

If I guessed right, you will be fine. If I not, you will get a ClassCastException and my apologies ;-))

Cheers ngomo

_________________
http://www.winfonet.eu


Top
 Profile  
 
 Post subject: Re: How to retrieve not a domain bean but some of its properties
PostPosted: Thu Apr 14, 2011 1:14 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello !

Thanks, it was the way to go.

It's funny because my integration test, which was already coded against the Dao method, worked fine.

This is how my Dao method looks like:

Code:
   @SuppressWarnings("unchecked")
   @Override
   public List<Object[]> findCountsByBrowser() {
      Query query = getSession().createQuery("select count(*) as count, visitorBrowser from StatisticsVisit group by visitorBrowser order by visitorBrowser");
      final List<Object[]> resultList = (List<Object[]>) query.list();
      return resultList;
   }


I was almost there, I only needed to add the Object[] type to the collection.

Now the question is, is this the way to go in Hibernate when encountering this issue ?

Maybe some day some one who stumbles by will tell us :-)

Thanks for your hint anyway.


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.