gpt4 book ai didi

java - 条件左外连接 SQL 查询到 JPQL

转载 作者:搜寻专家 更新时间:2023-10-30 19:46:16 27 4
gpt4 key购买 nike

SQL FIDDLE LINK FOR THIS DATA

什么是 jpql 等同于以下 SQL 查询:

select * from App left outer join App_Child on (App.id=App_Child.id and App_Child.status = 'active') where App.status='active' and App.id=1;

示例数据:

ij> select * from App;
ID |STATUS
----------------------
1 |active
2 |active1
3 |active3
5 |active

4 rows selected
ij> select * from App_Child;
ID |STATUS |D
----------------------------------
1 |active |1
2 |active11 |2
1 |active111 |3
1 |active |4

4 rows selected
ij> select * from App left outer join App_Child on (App.id=App_Child.id and App_Child.status = 'active') where App.status='active' and App.id=1;
ID |STATUS |ID |STATUS |D
---------------------------------------------------------
1 |active |1 |active |1
1 |active |1 |active |4

2 rows selected
ij> select * from App left outer join App_Child on (App.id=App_Child.id and App_Child.status = 'active') where App.status='active' and App.id=5;
ID |STATUS |ID |STATUS |D
---------------------------------------------------------
5 |active |NULL |NULL |NULL

1 row selected

编辑:我们正在使用 jpa 2.0

最佳答案

由于 @ManyToOne 关系,条件 App.id=App_Child.id 被自动添加。在 JPA 2.1 中,您可以使用显式 on 子句添加其他条件:

select a 
from App a left outer join
a.children c on (c.status = 'active')
where a.status='active' and a.id=1;

参见示例 EclipseLink Documentation .

关于java - 条件左外连接 SQL 查询到 JPQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13230956/

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