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中参数的语法),正如你从我的两个用法(:id and :days)中可以看到的那样.

但当我使用now()::date语句时,它是特定的postgreSQL语法,hibernate会 destruct 一切.

推荐答案

既然你是博士后,我会完全改变日期:

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

Postgresql相关问答推荐

org.postgresql. util.PSQLException:使用docker + docker—compose + kafka + springboot时try 连接失败

Select 所有数组类型和维度

PostgreSQL plpgsql函数问题

使用PythonPolar在PostgreSQL中存储

为什么我的唯一索引在 Postgresql 中不起作用?

INSERT 语句返回策略违规(USING 表达式)

如何在 kubernetes 中安全地重启 postgresql 容器?

如何在 postgresql 中创建一个空的 JSON 对象?

使用 JDBC 连接到 PostgreSql 的本地实例

在 Postgres 中查询 JSON 对象数组

PostgreSQL 如何比 SQLite 更快地执行写入?

等效于 Postgresql 中的 FOUND_ROWS() 函数

获取 PostgreSQL 中当前正在运行的查询的参数

在PostgreSQL中 Select 数组列的总和

如何使用 psql 命令列出、创建、使用和判断数据库?

django.db.utils.IntegrityError:duplicate key value violates unique constraint "django_content_type_pkey"

如何为查询执行设置语句超时

获取 psycopg2 count(*) 结果数

PostgreSQL 获取过go 12 小时的元素

docker postgres 无法从指定端口启动