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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7475876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:18:56  来源:igfitidea点击:

Using Hibernate query : colon gets treated as parameter / escaping colon

javahibernatespringpostgresqlhql

提问by Jaanus

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?

如何在 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).

基本上问题是我想在我的查询中使用冒号(:),但是当 hibernate 看到冒号时,它认为它是一个 paramter(:parameterName 是 HQL 中参数的语法),正如您从我的 2 个用途中看到的那样( :id and :days)。

But when I am using now()::date statement, it is specific postgreSQL syntax, hibernate ruins everything.

但是当我使用 now()::date 语句时,它是特定的 postgreSQL 语法,休眠会破坏一切。

采纳答案by atrain

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

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

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

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

回答by SwampDev

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: \\:

但是,在 Java 中,要\开始打印,您必须使用\.
所以,如果你想:在你的 SQL 休眠查询中放入一个,你必须这样写:\\:

And if you wanted to cast in PostgreSQL, such as in my case, you would have to, for example: field\\:\\:intif you wanted to cast some field as an integer.

如果你想在 PostgreSQL 中进行转换,比如在我的例子中,你必须,例如:field\\:\\:int如果你想将某个字段转换为整数。

回答by Jonathas Pacífico

Take a look at http://www.postgresql.org/docs/8.1/static/sql-createcast.html

看看http://www.postgresql.org/docs/8.1/static/sql-createcast.html

Try using cast. To me it worked like a charm.

尝试使用演员表。对我来说,它就像一种魅力。

回答by carlos.romel

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

回答by NimChimpsky

You escape :with ::. I think.

你逃脱:::。我认为。

Or try a nativequery

或者尝试一个nativequery

回答by stratwine

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

命名参数采用冒号 ':' 像这样是你要找的吗?