gpt4 book ai didi

java - 如何有效编写 JPA 命名查询

转载 作者:行者123 更新时间:2023-11-29 22:21:13 25 4
gpt4 key购买 nike

我已命名查询,如下所示:

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

两个命名查询之间的唯一区别是 c,c 的值可以为 null,并且 sql 查询的语法仅因此而不同。

有没有办法可以有效地将这两个语句结合起来?

最佳答案

使用这个:

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

关于java - 如何有效编写 JPA 命名查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30690928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com