-->
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.  [ 11 posts ] 
Author Message
 Post subject: count in HQL query that includes group by and having count
PostPosted: Tue Aug 23, 2005 3:18 am 
Newbie

Joined: Tue Aug 23, 2005 3:09 am
Posts: 3
Hi,

We are running the following query with HQL (it includes a main table and a set table inlcuded).

select distinct request.id from com.bmc.idm.ctsa.hb8.dbobject.Request as request join request.fields as fields where ((fields.attrName = 'jc_name' and fields.attrValue = 'xyz1') or (fields.attrName = 'user_id' and fields.attrValue like :'abc1') ) group by request.id having count(fields) = 2

We would like to execute select count that will return the count of the entries in the above query. We would appreciate any suggestion for HQL that wil run count for the queury above.
(our work around is to return the list.size() but this workaround has overhaed in performances)

Thanks.

Hibernate version is 3.0.5
This is the hbm file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.bmc.idm.ctsa.hb8.dbobject">
<class name="Request" table="REQUEST">
<id name="id" column="ID" type="long" unsaved-value="null">
<generator class="native">
<param name="sequence">REQUEST_seq</param>
</generator>
</id>
<version name="version" access="property"/>
<dynamic-component name="attributes">
<property name="requestor" column="REQUESTOR" access="property" type="string" not-null="true" />
<property name="subject" column="SUBJECT" access="property" type="string" not-null="true"/>
<property name="subEntityName" column="SUB_ENTITY" access="property" type="string" not-null="true" />
<property name="submissionDate" column="SUBMISSION_DATE" access="property" type="timestamp"/>
</dynamic-component>

<set name="fields" lazy="true" table="REQUEST_FIELDS" inverse="true" cascade="all-delete-orphan">
<key column="REQUEST_ID"/>
<one-to-many class="RequestField"/>
</set>
</class>

<class name="RequestField" table="REQUEST_FIELDS">
<id name="id" column="ID" type="long" unsaved-value="null">
<generator class="native">
<param name="sequence">REQUEST_FIELDS_seq</param>
</generator>
</id>
<many-to-one name="request" class="Request" not-null="true">
<column name="REQUEST_ID" unique-key="uq"/>
</many-to-one>
<property name="attrName" type="string">
<column name="FIELD_NAME" unique-key="uq"/>
</property>
<property name="attrValue" column="FIELD_VALUE" type="string" length="4000"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:18 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
You might test this one:
Code:
select count(distinct request.id) from com.bmc.idm.ctsa.hb8.dbobject.Request as request
join request.fields as fields
where ((fields.attrName = 'jc_name' and fields.attrValue = 'xyz1')
or     (fields.attrName = 'user_id' and fields.attrValue like 'abc1'))
group by request.id having count(fields) = 2


I don't know if it's supported by HQL.

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject: Re: count in HQL query that includes group by and having cou
PostPosted: Tue Aug 23, 2005 8:33 am 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
zohar wrote:
H
We would like to execute select count that will return the count of the entries in the above query. We would appreciate any suggestion for HQL that wil run count for the queury above.
(our work around is to return the list.size() but this workaround has overhaed in performances)


Use the HQL count function:

http://www.hibernate.org/hib_docs/v3/re ... hql-select

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:36 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
sven wrote:
You might test this one:
Code:
select count(distinct request.id) from com.bmc.idm.ctsa.hb8.dbobject.Request as request
join request.fields as fields
where ((fields.attrName = 'jc_name' and fields.attrValue = 'xyz1')
or     (fields.attrName = 'user_id' and fields.attrValue like 'abc1'))
group by request.id having count(fields) = 2


I don't know if it's supported by HQL.


It seems to be implemented:
http://www.hibernate.org/hib_docs/v3/re ... ggregation

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:49 am 
Newbie

Joined: Tue Aug 23, 2005 3:09 am
Posts: 3
Hi and Thanks,

The above suggestions don't work for us.
We were trying some count HQL queries, including the query:

Code:
select count(distinct request.id) from com.bmc.idm.ctsa.hb8.dbobject.Request as request
join request.fields as fields
where ((fields.attrName = 'jc_name' and fields.attrValue = 'xyz1')
or (fields.attrName = 'user_id' and fields.attrValue like 'abc1'))
group by request.id having count(fields) = 2


It is not helpful since it returns a list with many elemnts, where each element contains the count items in the group. This happens because we running a 'group by' + 'having count' clause.
The size of this list is real count we are looking for.
Other suggestions will be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:57 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Just leave out the GROUP BY clause. Duplicate entries are filtered using COUNT(DISTINCT...) and the GROUP BY is optional (even when using the HAVING clause). Though I think it's not recommended to use the HAVING clause with a GROUP BY.

Just test, if it satisfies your needs concerning performance.

Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 9:02 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
sven wrote:
Though I think it's not recommended to use the HAVING clause with a GROUP BY.


Sorry, "with = without"

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 9:06 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Alternatively, you might use a subselect construction:

Code:
select count(distinct req.id) from com.bmc.idm.ctsa.hb8.dbobject.Request as req
where req.id in

(select request.id from com.bmc.idm.ctsa.hb8.dbobject.Request as request
join request.fields as fields
where ((fields.attrName = 'jc_name' and fields.attrValue = 'xyz1')
or (fields.attrName = 'user_id' and fields.attrValue like 'abc1'))
group by request.id having count(fields) = 2)


Best regards
Sven

_________________
Please don't forget to give credit, if this posting helped to solve your problem.


Top
 Profile  
 
 Post subject: Re: count in HQL query that includes group by and having cou
PostPosted: Tue Aug 23, 2005 1:39 pm 
Senior
Senior

Joined: Tue Aug 03, 2004 2:11 pm
Posts: 142
Location: Somerset
If all else fails you can always use native SQL via hibernate, just make sure its portable.

_________________
On the information super B road


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 28, 2005 3:32 am 
Newbie

Joined: Tue Aug 23, 2005 3:09 am
Posts: 3
Thank you Sven.
The count query works. I will try to combine it in our code.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 18, 2007 7:22 pm 
Regular
Regular

Joined: Sat Jan 22, 2005 6:57 pm
Posts: 50
Location: Chicago
Does anyone know how to do this using Criteria objects and DetachedCriteria objects instead of HSQL?


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