使用 Hibernate 查询:冒号被视为参数/转义冒号

发布于 2024-12-05 06:49:14 字数 609 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

东走西顾 2024-12-12 06:49:14

我刚刚遇到了这个问题,不得不使用强制转换,所以我尝试了一些东西来使其工作。结果你在 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.

ι不睡觉的鱼゛ 2024-12-12 06:49:14

由于您在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();

请参阅“ nofollow”> http://www.postgresql.org/docs/8.2/static/functions-datetime.html

Since you're on Postgres, I would change the date() completely:

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();

See http://www.postgresql.org/docs/8.2/static/functions-datetime.html

望她远 2024-12-12 06:49:14

看看 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.

蓝礼 2024-12-12 06:49:14
return sessionFactory.getCurrentSession().
        createQuery("FROM Weather WHERE city_id = :id AND date " +
                "BETWEEN cast(now() as date) AND cast(now() as date) + (:days - 1)").
                setInteger("id", city_id).setString("days", days).list();
return sessionFactory.getCurrentSession().
        createQuery("FROM Weather WHERE city_id = :id AND date " +
                "BETWEEN cast(now() as date) AND cast(now() as date) + (:days - 1)").
                setInteger("id", city_id).setString("days", days).list();
不醒的梦 2024-12-12 06:49:14

命名参数取一个结肠':'就像你在找吗?

Named parameters take a colon ':' like this Is that what you were looking for ?

音盲 2024-12-12 06:49:14

您可以逃脱 ::。我认为。

或尝试a

You escape : with ::. I think.

Or try a nativequery

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文