Pregunta

I am working on a project where we have a collection of requests. I need to show the requests by their date of creation. However, I also need to show first those which have their due date expired. Here is the POJO class:

 @Entity
 @XmlRootElement
 @NamedQueries({
     @NamedQuery(name="Question.findAll", query="SELECT a FROM Request a ORDER BY a.created DESC"),
 })
 public class Request {

    Date created;
    Date duedate;
    String name;

    public Request () {}

    public static boolean isOverdue(){
         if duedate.before(new Date())) return true; else return false;
    }

    // omitting getter setters
 }

How can I write the JPQL statement in order to get the list of Requests ordered by date of creation but show first those which have their due date expired?

¿Fue útil?

Solución

I don’t know JPQL but I assume you can execute ordinary sql such as:

SELECT a FROM Request a ORDER BY coalesce(a.duedate, a.created) DESC

or

SELECT a FROM Request a ORDER BY coalesce(case when a.duedate < now() then duedate end, a.created) DESC

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top