如何有效地编写命名查询JPA

时间:2015-06-07 06:55:18

标签: java mysql jpa

我已经命名了如下所示的查询:

@NamedQueries({
    @NamedQuery(name = "table.getvalues", query = "select p from table p where p.a = :a and p.b = :b and p.c = :c order by id"),
    @NamedQuery(name = "table.getvalueswhencisnull", query = "select p from table p where p.a = :a and p.b = :b and p.c is null order by id")
})

2个命名查询之间的唯一区别是c的值,c可以为null,sql查询的语法因此而异。

有没有办法可以有效地支持这两种陈述?

2 个答案:

答案 0 :(得分:0)

使用它:

select p from table p where p.a = :a and p.b = :b and (p.c is null or p.c=:c) order by id

答案 1 :(得分:0)

也许这个过滤器会进行条件选择。

    SELECT                                   --             distinct branch_no,
      COUNT (DISTINCT member_no)
  FROM subscriptions                                       
 WHERE       
           su_sub_status NOT IN ('N', 'C')
       AND TRUNC (sub_Date) <= TO_DATE ('31/01/2014', 'dd/mm/yyyy')
       AND TRUNC (end_date) >= TO_DATE ('31/01/2014', 'dd/mm/yyyy')

--group by /* rollup */ (branch_no)