0

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
Tiny
  • 27,221
  • 105
  • 339
  • 599
Giovanny
  • 45
  • 1
  • 5
  • So, what's exactly your problem? It would help if you submit the whole query and the error you are getting when executing it (if there's one!) – Bonifacio Nov 17 '15 at 19:37
  • The query is using the time into consideration. how do I just compare date? – Giovanny Nov 17 '15 at 19:45
  • Oh I see... Have you tried the DATE function cast? DATE(o.myDate) >=:inputDate AND DATE(o.myDate) <=:inputDate. You should also make sure that your parameters are not using time. – Bonifacio Nov 17 '15 at 19:51

1 Answers1

0

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
Ish
  • 3,992
  • 1
  • 17
  • 23