-->
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.  [ 3 posts ] 
Author Message
 Post subject: select distinct on multiple properties not working in HQL
PostPosted: Wed Apr 15, 2009 6:51 pm 
Newbie

Joined: Tue Apr 07, 2009 6:02 pm
Posts: 6
I'm trying to do some simple reporting queries. I can get something to work in SQL quite easily - but in HQL there is one problem I am having with getting a distinct pair of values.

Code:
select count (distinct person, incident) from Incident as incident join incident.person as person


What I'm more interested in is something like this:
Code:
select count (distinct person, Month(incident.date)) from Incident as incident join incident.person as person


Even something as painfully simple as this wont work:
Code:
select distinct (person, incident) from Incident incident join incident.person


By not work, I mean I get these kind of errors (and no generated SQL - since it pukes on the query itself):
Code:
org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: {vector} [select distinct (person, incident) from com.foo.Incident incident join incident.person ]


The inability to make the distinct work the way it works in SQL (i.e. allow you to specify multiple things you want to be distinct) is making this basic reporting rather difficult.

Is it a synatx issue? is there some other way to do this? If it cant be done in HQL, can it be done in a criteria query?


Hibernate version:
3.3.1 GA

Name and version of the database you are using:
MySQL 5.1


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 15, 2009 10:36 pm 
Newbie

Joined: Tue Apr 07, 2009 6:02 pm
Posts: 6
Ok - I gave up on the HQL since the distinct element in it is only partially functional.

I can get it to kind of work using a raw SQL query that looks like this:

Code:
session.createSQLQuery(
   " select count(distinct Month(i.REPORTED_DATE), i.PERSON) as count, " +
   " Month(i.REPORTED_DATE) as theMonth, " +
   " i.*, p.*, up.* from  INCIDENT i join PHYSICIAN p on i.PERSON = p.USER_ID  inner join USER up on p.USER_ID=up.USER_ID group by  Month(i.REPORTED_DATE) "
).addScalar("count").addScalar("theMonth").addEntity("i",Incident.class).addEntity("p",Person.class).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();


the problem is that mixing the addScalar and addEntity doesn't work. I can use both of the scalars and get results, or I can use both of the entities and get results, but if I have all four in there - I get this error:

I'm testing the query in the Hibernate tools in Eclipse. This is the stacktrace in my console.log

Code:
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008] Sourced file: inline evaluation of: ``session.createSQLQuery(    " select count(distinct Month(i.REPORTED_DATE), i.ACT . . . '' : Method Invocation list : at Line: 5 : in file: inline evaluation of: ``session.createSQLQuery(    " select count(distinct Month(i.REPORTED_DATE), i.ACT . . . '' : .list ( ) 
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008] Target exception: java.lang.ArrayIndexOutOfBoundsException: 1
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.BSHPrimarySuffix.doName(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.BSHPrimarySuffix.doSuffix(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.BSHPrimaryExpression.eval(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.BSHPrimaryExpression.eval(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.Interpreter.eval(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.Interpreter.eval(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at bsh.Interpreter.eval(Unknown Source)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at org.hibernate.console.JavaPage.setSession(JavaPage.java:72)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at org.hibernate.console.ConsoleConfiguration$5.execute(ConsoleConfiguration.java:442)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at org.hibernate.console.execution.DefaultExecutionContext.execute(DefaultExecutionContext.java:64)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at org.hibernate.console.ConsoleConfiguration.executeBSHQuery(ConsoleConfiguration.java:437)
4/15/09 8:33:56 PM [0x0-0x1f81f8].org.eclipse.eclipse[9008]  at org.hibernate.eclipse.criteriaeditor.CriteriaEditor.executeQuery(CriteriaEditor.java:132)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 16, 2009 4:33 pm 
Newbie

Joined: Tue Apr 07, 2009 6:02 pm
Posts: 6
I solved that part too...

you have to specify the type:

eg:
.addScalar("timeUnit", Hibernate.INTEGER)


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