2014-05-12 110 views
0

我正在开发一个项目,其中包含一系列请求。我需要根据创建日期显示请求。不过,我也需要先显示那些到期日期到期的。这里是POJO类:按订单创建日期和截止日期的SQL订单

@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 
} 

我怎么能写JPQL声明,以获得创建日期进行排序的要求列表,但显示出第一是那些有自己的到期日期过期?

回答

1

我不知道JPQL但我相信你可以执行普通的SQL如:

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

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

+0

谢谢,它的工作原理。 JPQL还有COALESCE和CASE功能。 – Emin