HQL searches for a query by date (Java + NetBeans)

Hi everyone, I have the following problem. I have a reserve table in my MySQL database, date columns are defined by DATETIME. I need to make a request using sleep mode to find all reserves in one day, regardless of hour, only month and date of the same year, and I do it

public List<Reserve> bringAllResByDate(Date date){ em = emf.createEntityManager(); Query q = em.createQuery("SELECT r FROM Reserve r WHERE r.date=:date "); q.setParameter("date", date); 

...

I really don’t know how to do this, and will bring me only from the indicated date, any help?

+1
source share
2 answers

I'm sorry that there was no basic agnostic casting method for dates. I usually review your scenario.

 Calendar from = Calendar.getInstance(); Calendar to = Calendar.getInstance(); from.add(Calendar.DATE, -1); to.add(Calendar.DATE, 1); Query q = em.createQuery("SELECT r FROM Reserve r WHERE r.date > :dateFrom AND r.date < :dateTo "); q.setParameter("dateFrom", from.getTimeInMillis()); q.setParameter("dateTo", to.getTimeInMillis()); 
0
source
 Query q = em.createQuery( "SELECT r FROM Reserve r WHERE cast(r.date as date) = :date"); 

Please note that the underlying database must support the syntax ANSI cast(... as ...) .

+9
source

All Articles