-->
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: "is null" in select fails when there is a "group by" clause
PostPosted: Wed Sep 29, 2010 5:20 pm 
Newbie

Joined: Tue Oct 09, 2007 11:53 pm
Posts: 5
I have found one other post about this problem, but it had no replies. I'm hoping a better Subject will have better results.

I need to count the number of acknowledged and unacknowledged records that have data in a field.

The SQL query works:
Code:
select acknowledged, msgDate is null, count(*) from Record where company_id=27 group by acknowledged, msgDate is null


The HQL version My HQL query looks like this:
Code:
select acknowledged, msgDate is null, count(*) from Record where company=:comp group by acknowledged, msgDate is null


However, it fails with the following:
Code:
java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: is null


If I try to use an alias, I get:
Code:
select acknowledged, msgDate is null as hasMsg, count(*) from Record where company=:comp group by acknowledged, hasMsg

ERROR org.hibernate.hql.PARSER(1):33 - <AST>:0:0: unexpected AST node: is null

which is followed by a NullPointerException in HqlSqlWalker.setAlias().

If I try to use field numbers in the group by clause, I get :
Code:
select acknowledged, msgDate is null, count(*) from Record where company=:comp group by 1,2

Unknown column '2' in 'group statement'


I've tried sticking it in a subquery and in a "new Map", but I always get one of the errors mentioned above.

I can move it to the where clause and just execute it twice, but I don't want to make an extra trip to the DB, and IT SHOULD WORK, DARN IT! :-/

I'm using MySQL 5.0.37 and Hibernate 3.2.4.


Top
 Profile  
 
 Post subject: Re: "is null" in select fails when there is a "group by" clause
PostPosted: Thu Sep 30, 2010 1:21 am 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
Funny syntax IMHO. I assume you specified MySQL dialect in hibernate settings. Try looking into the code and figure out why AST is not able to parse/generate code correctly.


Top
 Profile  
 
 Post subject: Re: "is null" in select fails when there is a "group by" clause
PostPosted: Thu Sep 30, 2010 1:25 am 
Beginner
Beginner

Joined: Fri Mar 11, 2005 7:46 am
Posts: 29
Again, I don't use MySQL - but do you need to use "smth is null" in group by clause? I would expect just column name there, without any expressions, eg.

Code:
select acknowledged, msgDate is null, count(*) from Record where company_id=27 group by acknowledged, msgDate


From what I've found in mysql doco, IS NULL is an expression and IMO you should not tell database to group by an expression, it should be a column or possibly concatenation of columns, but not smth. like "column1 is null"


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.