¿Cómo agrego una fecha y luego la comparo con la fecha actual en Hibernate?

I started with the following Hibernate code:

@SuppressWarnings("unchecked")
List<Event> events = (List<Event>) session.createQuery(
    "FROM Event e WHERE e.enabled = TRUE e.date + 31557600 < :now"
)
.setDate("now", new Date())
.list();

Adding to the date did not work. I looked into using Criterios con Restricciones, but I also need to check that the event is enabled. How can I accomplish this date comparison?

preguntado el 09 de septiembre de 13 a las 22:09

isn't the same if you do the math on the :now value that you pass in java instead than doing the operation on the database? -

Good idea! I'm heading out but if you can write code that works I'll accept your answer. -

1 Respuestas

Calendar currDate = Calendar.getInstance();
currDate.add(<field>, -31557600); // I guess field will be Calendar.MILLISECOND 

and then on your query you write

@SuppressWarnings("unchecked")
List<Event> events = (List<Event>) session.createQuery(
    "FROM Event e WHERE e.enabled = TRUE e.date > :now"
)
.setDate("now", currDate.getTime())
.list();

¿Esto funciona?

Respondido el 09 de Septiembre de 13 a las 23:09

It worked! It seems so obvious now, that lack of sleep didn't do me very well :D. I made some edits to your answer that make it more clear. - NobleUplift

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.