使用 Hibernate 查询:冒号被视为参数/转义冒号
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();
收到错误:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: :
如何在 HQL 中使用此语法?
基本上问题是我想在查询中使用冒号(:),但是当hibernate看到冒号时,它认为它是一个参数(:parameterName是HQL中参数的语法),正如你从我的2次使用中看到的( :id 和:days
)。
但是当我使用 now()::date 语句时,它是特定的 postgreSQL 语法,hibernate 毁了一切。
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();
getting error:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: :
How can I use this syntax in HQL?
Basically the problem is that I want to use colon(:) in my query, but when hibernate sees colon, it thinks that it is a paramter(:parameterName is syntax for parameters in HQL), as you can see from my 2 uses(:id and :days
).
But when I am using now()::date statement, it is specific postgreSQL syntax, hibernate ruins everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我刚刚遇到了这个问题,不得不使用强制转换,所以我尝试了一些东西来使其工作。结果你在 hibernate 中用
\
转义 :但是,在 java 中,要首先打印
\
,你必须用\
转义它。因此,如果你想在 SQL hibernate 查询中添加
:
,你必须这样写:\\:
如果你想在 PostgreSQL 中进行转换,例如就我而言,如果您想将某些字段转换为整数,则必须:
field\\:\\:int
。I just had this problem, had to use casts, so I tried some stuff to make it work. Turns out you escape : in hibernate with
\
However, in java, to print
\
to begin with, you have to escape it with\
.So, if you want to put a
:
in your SQL hibernate query, you have to write it like:\\:
And if you wanted to cast in PostgreSQL, such as in my case, you would have to, for example:
field\\:\\:int
if you wanted to cast some field as an integer.由于您在Postgres上,我会完全更改date():
请参阅“ nofollow”> http://www.postgresql.org/docs/8.2/static/functions-datetime.html
Since you're on Postgres, I would change the date() completely:
See http://www.postgresql.org/docs/8.2/static/functions-datetime.html
看看 http:///www.postgresql.org/8.11/8.11/8.11 /static/sql-createcast.html
尝试使用铸件。对我来说,它像魅力一样工作。
Take a look at http://www.postgresql.org/docs/8.1/static/sql-createcast.html
Try using cast. To me it worked like a charm.
命名参数取一个结肠':'就像你在找吗?
Named parameters take a colon ':' like this Is that what you were looking for ?
您可以逃脱
::
。我认为。或尝试a
You escape
:
with::
. I think.Or try a nativequery