使用Hibernate查询:冒号被视为参数/转义冒号 [英] Using Hibernate query : colon gets treated as parameter / escaping colon

查看:290
本文介绍了使用Hibernate查询:冒号被视为参数/转义冒号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 返回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:意外令牌::

我可以在HQL中使用这种语法吗?基本上问题是我想在我的查询中使用冒号(:),但是当hibernate看到冒号时,它认为它是一个参数(:parameterName是语法对于HQL中的参数),你可以从我的2次使用中看到(:id和:days )。

但是,当我使用now():: date语句时,它是特定的postgreSQL语法,hibernate会破坏所有内容。

'在Postgres上,我会完全改变date():

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

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


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.

解决方案

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

这篇关于使用Hibernate查询:冒号被视为参数/转义冒号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆