Date only comparison without time using JPA named query

不打扰是莪最后的温柔 提交于 2019-12-12 01:17:40

问题


My database has a column that's DATE.

When generated the entity, it created a Timestamp field in the entity class.

@Column(name="MY_DATE")
private Timestamp myDate;

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

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.

Query condition

o.myDate >=:inputDate AND o.myDate <=:inputDate

回答1:


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;

In your query, you can use BETWEEN instead of comparison operators:

o.myDate BETWEEN :startDate AND :endDate


来源:https://stackoverflow.com/questions/33763966/date-only-comparison-without-time-using-jpa-named-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!