gpt4 book ai didi

java - HQL(hibernate) 时间戳范围匹配

转载 作者:行者123 更新时间:2023-11-29 07:22:02 27 4
gpt4 key购买 nike

我需要编写一个查询来获取某个时间范围内的对象,目前查询如下所示:

Timestamp from = ... 
Timestamp to = ...

getHibernateTemplate().find("from " + Person.class.getName() + " ml where ml.lastModifiedOn>="+from.toString()+" and m1.lastModifiedOn<=" + to.toString());

但是,由于显而易见的原因,这不起作用。我如何格式化查询可以接受的时间戳。

org.springframework.orm.hibernate3.HibernateQueryException:意外标记:16 靠近第 1 行,第 123 列 [来自 Person ml where ml.lastModifiedOn>=2010-02-12 16:00:21.292 and m1.lastModifiedOn

最佳答案

您在当前查询中缺少单引号。以下应该有效:

from Person ml where ml.lastModifiedOn 
between '2010-02-12 16:00:21.292' and '2010-02-12 23:00:21.292'

请注意,我不知道您为什么不将 Date 实例传递给以下查询:

from Person ml where ml.lastModifiedOn between :from and :to 

你在这里使用 java.sql.Timestamp 吗?如果是,则您不应该这样做。

关于java - HQL(hibernate) 时间戳范围匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2864925/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com