我想将我的postgresql查询映射到java中的jpql查询。我不能映射"created AT time zone 'UTC‘AT time zone 'Europe/Paris'“。
我的postgresql查询:
SELECT count(*), to_char(created AT time zone 'UTC' AT time zone 'Europe/Paris','DD-MM-YYYY')
from my_table GROUP BY to_char(created AT time zone 'UTC' AT time zone 'Europe/Paris','DD-MM-YYYY');我的jpql查询:
Query query = em.createQuery("select count(z), to_char(z.created, 'DD-MM-YYYY'), z.formType from MyTableEntity z where z.created >= :startFrom and z.created <= :endTo GROUP BY to_char(z.created, 'DD-MM-YYYY'), z.formType ");如何按我在时间戳'Europe/Paris‘中创建的字段分组?在数据库中创建的字段保存在时间戳中。在jpql中的时区不工作。
发布于 2019-04-10 16:39:52
您根本不应该使用to_char将日期转换为字符串。让JPA处理转换。此外,PostgreSQL可以存储时间戳,但不存储时区。它只存储给定时刻的时区偏移量。
要按日期获得这些结果,您可以执行强制转换为日期,而不是使用格式化。下面是它作为原生查询的样子:
SELECT count(*), cast(created AS date), form_type from my_table
GROUP BY cast(created AS date), form_type;在JPQL中:
SELECT count(z), cast(z.created as date), z.formType from MyTableEntity z
where z.created between :startFrom and :endTo
GROUP BY cast(z.created as date), z.formTypea between x and y语句等同于a >= x and a <= y。
如果您的数据库运行在默认时区为Europe/Paris的服务器上,则不需要进行时区转换。或者,您可以运行本机查询来设置当前连接https://www.postgresql.org/docs/9.1/sql-set.html的时区,然后运行其他查询。
SET SESSION TIME ZONE 'Europe/Paris'https://stackoverflow.com/questions/55607403
复制相似问题