使用JPA命名查询的仅日期比较,没有时间 [英] Date only comparison without time using JPA named query

查看:553
本文介绍了使用JPA命名查询的仅日期比较,没有时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库的列为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对象. startDateendDate.

My incoming object is a java.util.Date object. startDate and endDate.

我需要编写一个JPA查询,该查询将获取在startDateendDate之间具有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命名查询的仅日期比较,没有时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆