Hibernate 日期查询示例
我正在使用 Hibernate 的 Example
类来查询与示例不匹配的对象。只要我的休眠类型中没有日期,一切都会正常工作。但是,当我的 Hibernate 类型中有日期时,Example
永远不会匹配。 (因为它不是,所以我总是收到不应出现在查询中的对象。)
这是我正在执行的代码,
Criteria criteria = databaseSession.createCriteria("Person");
criteria.add(Restrictions.not(Example.create(sqlEntity).excludeNone()));
List entities = criteria.list();
// entities contains more objects than it should
如果我从 hbm.xml 文件中注释掉日期属性,则查询将按预期工作。
<class entity-name="Person">
...
<!-- This property is causing the Example to not match. -->
<property name="date" type="date">
<column name="Date"/>
</property>
</class>
sqlEntity
对象具有日期精度(即时间值为 00:00:00)。这就是 sqlEntity
对象中提供的全部内容。这也是为什么日期属性具有日期类型而不是时间戳的原因。 SQL 表中的实际行具有毫秒精度和时间,不等于 00:00:00。
如何在具有毫秒精度的日期时间列上执行此示例查询?
顺便说一句,我调试了 sqlEntity
的日期值以及来自 entities
的对象,这些对象由于日期的原因不应出现。对这两个对象调用 Object.equals()
将返回 true
。
I am using Hibernate's Example
class to query for objects which do not match the example. Everything works fine so long as I do not have dates in my Hibernate type. However, when I have dates in my Hibernate type, the Example
never matches. (Since it is a not, I always receive objects which should not be present in the query.)
This is the code I am executing
Criteria criteria = databaseSession.createCriteria("Person");
criteria.add(Restrictions.not(Example.create(sqlEntity).excludeNone()));
List entities = criteria.list();
// entities contains more objects than it should
If I comment the date property out of my hbm.xml file the query works as expected.
<class entity-name="Person">
...
<!-- This property is causing the Example to not match. -->
<property name="date" type="date">
<column name="Date"/>
</property>
</class>
The sqlEntity
object has a date-precision (i.e. the time value is 00:00:00). This is all that is provided into the sqlEntity
object. That is also why the date property has a type of date rather than timestamp. The actual rows in the SQL table have millisecond precision and times which are not equal to 00:00:00.
How can I perform this Example query with a date precision on a datetime column with millisecond precision?
By the way, I debugged the date value of the sqlEntity
and the objects from entities
which should not have been present because of dates. Calling Object.equals()
on those two objects returns true
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论