gpt4 book ai didi

java - JPA 中多列的不同

转载 作者:行者123 更新时间:2023-12-01 05:06:19 25 4
gpt4 key购买 nike

我有一个 courier_routes 表,其中有两个指向位置表的外键。 courier_routes 表中的列是 start_location_id 和 end_location_id。 (我使用的是 Oracle)

我想在 courier_routes 表中找到 express 员去过的所有不同位置。

考虑这个例子

|id |start_location_id| end_location_id|...
|1|1|2|
|2|1|3|
|3|4|5|
|4|2|1|

我想取回 ids 1 2 3 4 5

我的 SQL 有效的是

SELECT loc FROM (
SELECT start_location_id AS loc FROM courier_routes
UNION
SELECT end_location_id AS loc FROM courier_routes);

我想将其翻译成 JPA。 Hibernate 不支持联合,所以我考虑做子查询。这样做的问题是它可能会影响性能。

我正在考虑类似(伪代码...)的东西

SELECT id FROM locations where id in (
(SELECT start_location_id FROM courier_routes)
OR
(SELECT end_location_id FROM courier_routes));

有没有更简单的方法?这个表会变得很大,所以我真的不想使用子查询。

我应该通过将两个单独的查询结果添加到一个集合中来在 java 中进行联合吗?

谢谢

最佳答案

您可以在 HQL 中对两列进行联接:

SELECT distinct loc.id FROM locations loc, courier_routes c 
where loc.id=c.start_location_id OR c.id=c.end_location_id

希望这有帮助!

关于java - JPA 中多列的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12616781/

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