-->
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: Having count(distinct a,b,c,...
PostPosted: Fri Dec 05, 2008 6:16 pm 
Newbie

Joined: Fri Dec 05, 2008 5:48 pm
Posts: 3
Hello.

I want to realize a statement in HQL or Criteria which works in SQL. The statement returns all mediator(id)s which have more than 250 orders. A order is a combination of the order date (co.created) and the id of a indent or a contract.

I don't know the exactly SQL code now but this pseudo code should explain what i mean.

<code>
select distinct co.mediator_id from COMMISSION co
...
group by co.mediator_id
having count( distinct co.created, isNull(co.indent_id, co.contract_id) ) > 250
</code>

Does anyone know how to realize this?

Best regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 06, 2008 12:14 am 
Regular
Regular

Joined: Tue Sep 26, 2006 11:37 am
Posts: 115
Location: Sacramento, CA
I think you can approach this in 2 steps:

1. on your pojo define the order as a property and for the mapping use a formula, the formula will implement in sql your predicate
http://www.hibernate.org/hib_docs/annotations/api/org/hibernate/annotations/Formula.html

2. in HQL/Criteria use the property name that you defined above

This approach has the added benefit of explicitly exposing the concept of 'hasAnOrder' and this may prove useful in other parts of the design AND/OR ease maintenance moving forward.

Marius


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 06, 2008 6:17 am 
Newbie

Joined: Fri Dec 05, 2008 5:48 pm
Posts: 3
I'm not so familiar with Hibernate so let see if I got this right.

Now I have 3 Java classes (Commission, Ident, Contract). Each of them is mapped with Hibernate to a DB table.

I create a new Java class (Order) with 3 attributes (mediator_id,created, order_id) which is not mapped to a DB table but mapped with a formular in native SQL.
Right?

For my example the SQL should look like this?

<code>
select distinct co.mediator_id, co.created, isNull(co.indent_id, co.contract_id) from COMMISSION co
</code>

Now the Order can be used in the having/count part, correct?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2008 3:01 pm 
Newbie

Joined: Fri Dec 05, 2008 5:48 pm
Posts: 3
It's working now. Thanks Marius.


Top
 Profile  
 
 Post subject: Re: Having count(distinct a,b,c,...
PostPosted: Fri Oct 02, 2009 1:16 pm 
Newbie

Joined: Tue Oct 17, 2006 7:07 pm
Posts: 4
Just so that there's a record when the next person tries to ask this, yes: "having count(distinct user.foo)" works, but "having count(distinct lower(user.foo))" does not.


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.