-->

Date only comparison without time using JPA named

2019-08-29 00:17发布

问题:

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