[Hibernate-JIRA] Created: (HHH-6024) NPE in CastFunction when casting a field named "date" to type "date" in HQL
by Jon Mann (JIRA)
NPE in CastFunction when casting a field named "date" to type "date" in HQL
---------------------------------------------------------------------------
Key: HHH-6024
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6024
Project: Hibernate Core
Issue Type: Bug
Affects Versions: 3.6.2
Environment: Hibernate 3.6.0.Final and 3.6.2.Final
MySQL 5.0
Reporter: Jon Mann
Attachments: hibernate-cast-date.zip
In Hibernate version "3.6.2.Final" when casting a field named "date" to type "date":
SELECT entity FROM DateEntity AS entity WHERE CAST(entity.date AS date) = ?
we encounter a NullPointerException in CastFunction:
java.lang.NullPointerException
at org.hibernate.dialect.function.CastFunction.render(CastFunction.java:56)
at org.hibernate.hql.ast.SqlGenerator.endFunctionTemplate(SqlGenerator.java:214)
at org.hibernate.hql.antlr.SqlGeneratorBase.methodCall(SqlGeneratorBase.java:2325)
...
If we rename the field "date" to "creationDate", the problem dissapears.
If we cast to "time" instead of "date", the problem dissapears.
We encountered this problem while using Hibernate version "3.6.0.Final" against MySQL 5.0.
Upgrading to Hibernate version "3.6.2.Final" did not resolve the problem.
We guess this problem is a bug in the Hibernate HQL parsing code. It might be a problem with our usage of Hibernate, in which case it would be helpful for Hibernate to return a useful error message.
Please find attached a minimal Maven test case (using HSQL memory DB) which demonstrates the problem.
The following JIRA issue maybe related to this problem:
HHH-3770 - NPE in o.h.dialect.function.CastFunction
Credit to my colleague Sergey Shinkevich for creating the test case.
Thanks to the Hibernate team for a great product.
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
13 years, 2 months
[Hibernate-JIRA] Created: (HHH-6385) Order by avg() does not use function registered by dialect (e.g. AvgWithArgumentCastFunction)
by Robin Sander (JIRA)
Order by avg() does not use function registered by dialect (e.g. AvgWithArgumentCastFunction)
---------------------------------------------------------------------------------------------
Key: HHH-6385
URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-6385
Project: Hibernate Core
Issue Type: Bug
Affects Versions: 3.6.5
Environment: Java 6, JPA 2, HsqlDB 2
Reporter: Robin Sander
Priority: Minor
Some dialects register a special function for the aggregate function 'avg'. For example, HSQLDialect calls {{registerFunction("avg", new AvgWithArgumentCastFunction("double"))}}
which is used if {{avg}} appears in a select statement but not if {{avg}} appears in an {{order by}} statement which leads to wrong results.
For exampe, the following query
{code}
SELECT v.target.id, count(v), avg(v.rating) FROM Vote v group by v.target.id having count(v) >= 3
ORDER BY avg(v.rating) desc, count(v) desc
{code}
gets translated into
{code}
select vote0_.target as col_0_0_, count(vote0_.id) as col_1_0_, avg(cast(vote0_.rating as double)) as col_2_0_
from vote vote0_ group by vote0_.target having count(vote0_.id)>=3 order by avg(vote0_.rating) desc,
count(vote0_.id) desc
{code}
So the workaround is to use an explicit cast in the order by clause:
{code}
SELECT v.target.id, count(v), avg(v.rating) FROM Vote v group by v.target.id having count(v) >= 3
ORDER BY avg(cast(v.rating as double)) desc, count(v) desc
{code}
This gets translated into the correct SQL query:
{code}
select vote0_.target as col_0_0_, count(vote0_.id) as col_1_0_, avg(cast(vote0_.rating as double)) as col_2_0_
from vote vote0_ group by vote0_.target having count(vote0_.id)>=3 order by avg(cast(vote0_.rating as double)) desc,
count(vote0_.id) desc
{code}
While this workaround is pretty ease, I've stumbled across this bug while using a JPA criteria query for the same purpose. With the criteria query I had to use {{cb.avg(vote.get(Vote_.rating).as(Double.TYPE))}} as a workaround but unfortunately this led to a NPE in {{org.hibernate.dialect.function.CastFunction.render(CastFunction.java:56)}} (maybe because of HHH-6024).
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
13 years, 2 months