我通过JPA使用Hibernate,并且需要按照其Date
属性的时间部分筛选记录。JPA和Hibernate:仅比较时间戳的部分时间
public class Foo {
Date getDateTime() {
// Returns a date and time value.
}
}
Time startTime = ...;
Time endTime = ...;
TypedQuery<Foo> query = entityManager.createQuery("SELECT foo FROM Foo foo where :startTime <= foo.dateTime AND foo.dateTime <= :endTime", Foo.class);
query.setParameter("startTime", startTime);
query.setParameter("endTime ", endTime);
当然,这是行不通的,因为你不能直接比较一个Date
和Time
。如果这是一个SQL查询,那么我会使用几个DBMS特定的日期函数来比较它们,但是如何比较JPA中的日期和时间?
您使用java.sql.Time吗? –
这是正确的。 –
只是为了澄清...您想要查询具有日期时间列的记录,但只能与该列的hh:mm:ss.SSS组件进行比较。或者换句话说,“给我一天中发生的所有记录”。正确? – kaliatech