-->
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.  [ 2 posts ] 
Author Message
 Post subject: Perform aggregate count and encapsulate into value object
PostPosted: Thu Apr 07, 2005 12:49 am 
Newbie

Joined: Thu Apr 07, 2005 12:34 am
Posts: 2
Hi guys.

I am new to Hibernate and just started playing around with it yesterday. Well I have to admit it is definitely one of the best persistence solution today.

I need some help here though.

The scenario is this:
One Registrant registers for one GolfSession. One GolfSession can be registered by many registrants. I am trying to retrieve the count of Registrants that have registered for each GolfSession.

Hibernate version:
2.1

Mapping documents:
Code:
<class name="Registrant" table="Registrant">
      <meta attribute="extends">BaseModel</meta>
      <meta attribute="class-description">
         Represents a single Registrant in the database
         @author Zach Lim (with help from Hibernate)
      </meta>

      <id name="id" type="long" unsaved-value="null">
         <generator class="identity"/>
      </id>

   <many-to-one name="golfSession" class="GolfSession" column="golfSession_id" />

   ......................
   ......................

   </class>

   <class name="GolfSession" table="GolfSession">
      <meta attribute="extends">BaseModel</meta>
      <meta attribute="class-description">
         Represents a single GolfSession in the database.
      </meta>

      <id name="id" type="int" unsaved-value="null">
         <generator class="identity"/>
      </id>
      <property name="dateString" type="string"/>
      <property name="description" type="string"/>
      <property name="slotsTotal" type="integer"/>
   </class>


Here's the Value object below

Code:
/**
*  Description of the Class
*
* @author     zach
* @created    April 7, 2005
*/
public class GolfSessionVO {

   private GolfSession golfSession;
   private Integer takenSlots;


   /**
    *  Constructor for the GolfSessionVO object
    *
    * @param  golfSession  Description of the Parameter
    * @param  booked       Description of the Parameter
    */
   public GolfSessionVO(GolfSession golfSession, Integer takenSlots) {
      this.golfSession = golfSession;
      this.takenSlots = takenSlots;
   }
}



My Code
I am using Spring's HibernateDaoSupport
Code:
   /**
    *  Gets the golfSessionVO attribute of the RegistrantDAOHibernate object
    *
    * @return    The golfSessionVO value
    */
   public List getGolfSessionVO() {
      return getHibernateTemplate().find(
            "select GolfSessionVO(golf, count(registrant.id)) " +
            "from Registrant as registrant, Golf as golf group by golf.id"
            );
   }



Full stack trace of any exception that occurs:
unexpected token: as ..........

I got a feeling my query is wrong. How do I do it such that I can get a List of GolfSessionVO encapsulating the GolfSession and the registration count.

I have been stuck for 1 day. Please help me guys.

Thanks.
Zach


Top
 Profile  
 
 Post subject: solution
PostPosted: Thu Apr 07, 2005 2:19 am 
Newbie

Joined: Thu Apr 07, 2005 12:34 am
Posts: 2
Hey guys. I found the solution.

Code:
/**
    *  Gets the golfSessionVO attribute of the RegistrantDAOHibernate object
    *
    * @return    The golfSessionVO value
    **/
   public List getGolfSessionVO() {
      
      return getHibernateTemplate().find("select new GolfSessionVO(golf, count(registrant)) " +
          "from Registrant as registrant " +
          "right join registrant.golfSession as golf "+
          "group by golf");

   }


Note that we have to perform a right join as a left join will ignore all GolfSessions which have not been registered.

Hibernate rocks!

Warmest regards,
Zach


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