Jaa*_*nus 23 java postgresql spring hibernate hql
return sessionFactory.getCurrentSession().
createQuery("FROM Weather WHERE city_id = :id AND date " +
"BETWEEN now()::date AND now()::date + (:days - 1)").
setInteger("id", city_id).setString("days", days).list();
Run Code Online (Sandbox Code Playgroud)
得到错误:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: :
Run Code Online (Sandbox Code Playgroud)
如何在HQL中使用此语法?
基本上问题是我想在我的查询中使用冒号(:),但是当hibernate看到冒号时,它认为它是一个参数(:parameterName是HQL中参数的语法),正如你从我的2个用途中看到的那样(:id and :days).
但是当我使用now():: date语句时,它是特定的postgreSQL语法,hibernate破坏了一切.
小智 41
我刚遇到这个问题,不得不使用强制转换,所以我尝试了一些东西来使它工作.结果你逃脱:在休眠中\
但是,在java中,要打印\开始,你必须用它来逃避它\.
所以,如果你想:在你的SQL hibernate查询中加入一个,你必须写它:\\:
如果你想在PostgreSQL中进行转换,例如在我的情况下,你将不得不,例如:field\\:\\:int如果你想将一些字段转换为整数.
既然你在 Postgres 上,我会完全改变 date() :
return sessionFactory.getCurrentSession().
createQuery("FROM Weather WHERE city_id = :id AND date " +
"BETWEEN current_date AND (current_date + (integer :days - 1))").
setInteger("id", city_id).setString("days", days).list();
Run Code Online (Sandbox Code Playgroud)
请参阅http://www.postgresql.org/docs/8.2/static/functions-datetime.html
| 归档时间: |
|
| 查看次数: |
17906 次 |
| 最近记录: |