问题描述
我的数据库的列为DATE
.
生成实体时,它在实体类中创建了一个Timestamp
字段.
When generated the entity, it created a Timestamp
field in the entity class.
@Column(name="MY_DATE")
private Timestamp myDate;
我的传入对象是java.util.Date
对象. startDate
和endDate
.
My incoming object is a java.util.Date
object. startDate
and endDate
.
我需要编写一个JPA查询,该查询将获取在startDate
和endDate
之间具有my_date
的记录.这是需要考虑的时间.
I need to write a JPA query that will fetch the records that has my_date
between startDate
and endDate
. It is taking time into consideration.
查询条件
o.myDate >=:inputDate AND o.myDate <=:inputDate
推荐答案
您的实体代码是否自动生成?也许您应该尝试更新生成的实体代码.将myDate
字段的类型更改为Date
而不是java.sql.Timestamp
.然后用@Temporal
注释它,并指定DATE
作为类型,如下面的代码所示:
Is your entity code auto-generated? Maybe you should try updating your generated Entity code. Change the type of myDate
field to Date
instead of java.sql.Timestamp
. Then annotate it with @Temporal
and specify DATE
as the type, as shown in the code below:
@Column(name="MY_DATE")
@Temporal(TemporalType.DATE)
private Date myDate;
在查询中,您可以使用BETWEEN
代替比较运算符:
In your query, you can use BETWEEN
instead of comparison operators:
o.myDate BETWEEN :startDate AND :endDate
这篇关于使用JPA命名查询的仅日期比较,没有时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!