-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL question - count
PostPosted: Tue Jun 14, 2005 12:49 pm 
Newbie

Joined: Tue Jun 14, 2005 12:44 pm
Posts: 19
Hello
Using the latest Hibernate - quick question about HQL query using count:

I have two tables - Sessions and Messages. I run a from query on both of them although I mostly retrieve Session data.
The two tables have a foreign key relationship on sessionId - Messages have sessionIDs which tell them which sessions they contain (each msg contains 1 session). Sessions do not have message information.

I would like to do a simple count that tells me how many times a certain session occurs inside all the messages, while querying the entire Sessions table (using from Session).

Can anyone direct me to a solution? I cannot make this query work.

Best regards
Eyal


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 2:15 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
I'm basically trying to do the same thing and can't get it to work either. Also, I would like to save the count value as a property in my pojo, but it's not a mapped column, so I'm not sure how to do this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 2:27 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
graeder wrote:
I'm basically trying to do the same thing and can't get it to work either. Also, I would like to save the count value as a property in my pojo, but it's not a mapped column, so I'm not sure how to do this.



There's an example using a Filter in the h3 docs under the Improving performance section. I use queries like the one below to test for the existence of objects. I think I got the original code from the 2.1.x docs.

I define a NamedQuery in my mapping file.
Code:
  <query name="MyObject.existsQuery> <![CDATA[
select count(*) from MyObject myobj where myobj.m2o.id = :manyToOneId
    ]]>
  </query> 

And the Java Code looks like this.
Code:
   Query q = session.getNamedQuery("MyObject.existsQuery");
   q.setLong(InterventionSessionDVO.SESSION_ID, sessionId.longValue());
   int count = ((Integer) q.iterate().next()).intValue();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 2:53 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
is it somehow possible to map the "count" value to a property in the pojo?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 14, 2005 3:06 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
graeder wrote:
is it somehow possible to map the "count" value to a property in the pojo?


check out section 6.1.9 of the 3.0.5 docs. You can map a property to a formula which is an arbitrary SQL expression.

I haven't used this feature myself but it sounds like what you're looking for.


Top
 Profile  
 
 Post subject: further question
PostPosted: Wed Jun 15, 2005 2:22 am 
Newbie

Joined: Tue Jun 14, 2005 12:44 pm
Posts: 19
Thanks for the replies people.
However my query requires me to find this count for *each* item in my result list. Thus, I would like to initialize my count(*) function with a paramater that I receive in each item - the SessionID:

Code:
"select new SessionSummary (session.sessionId,session.Started,session.Ended, [b]count(*) from message where sessionId=session.SessionId[/b] ) from SessionObj session, MessageObj message"


I understand that it is possible to do this type of count query with a single ID as input, but how can I use the sessionID I receive on the select clause from SessionObj to run the count query?

10x
Eyal


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