Reputation: 186
I tried to pass date using following way but unable to succeed. dateAnswered is field which stores date as 2017-09-13 00:00:00.
Can anyone tell where i am getting wrong:
Way 1: SetParameter
@NamedQuery(name = "findAnswar", query = "SELECT a FROM Answar a WHERE (a.dateAnswered = :currentDate )"
Try 1:
super.em.createNamedQuery( "findAnswar" , Answar .class ).setParameter( "currentDate", new Date() ); //Not receive any data
Try 2:
Date date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
super.em.createNamedQuery( "findAnswar" , Answar .class ).setParameter( "currentDate", date) //Not receive any data
Way 2: Set inbuilt parameters
@NamedQuery(name = "findAnswar", query = "SELECT a FROM Answar a WHERE (a.dateAnswered = current_date() )") //NOT WORKS
It works if i do add Temporal to getter:
@Column(name = "DATE_ANASWERED")
@Temporal(TemporalType.DATE)
public Date getDateAnswered () {
return dateAnswered ;
}
BUT IT GIVES another issue : Caused by: org.hibernate.HibernateException: Wrong column type in db.ANSWAR for column DATE_ANASWERED. Found: datetime, expected: date
@NamedQuery(name = "findAnswar", query = "SELECT a FROM Answar a WHERE (a.dateAnswered = current_date )") //NOT WORKS - JPA errors
Even in previous answers of Stackoverflow i am not able to find any related solution. Can anyone help in fixing the same using Named Query only?
Upvotes: 1
Views: 1569
Reputation: 186
Able to achieve it doing following changes to named query and it worked like charm:
"SELECT a FROM Answar a WHERE cast(a.dateAnswered as date) = current_date()
As underlying type is datetime so so we need to cast it to date to use current_date()
Upvotes: 2
Reputation: 1027
You need to specify the temporal type.
Way 1
super.em.createNamedQuery("findAnswar", Answar.class)
.setParameter("currentDate", new Date(), TemporalType.TIMESTAMP);
Way 2
@Column(name = "DATE_ANASWERED")
@Temporal(TemporalType.TIMESTAMP)
public Date getDateAnswered () {
return dateAnswered ;
}
Upvotes: 0