【发布时间】:2017-03-17 14:55:41
【问题描述】:
我的问题是我如何在 JPA 中执行 UNION ALL 子句? 可以使用@NamedQuery 吗?此查询在每个 SELECT 语句中一次使用 2 个表,最后它使用 ORDER BY
SELECT 0 as intorder, u.id, q.order_type
FROM test_union u, test_query q
WHERE u.id = q.id
AND u.type is NULL
AND q.field1 = 'DEFAULTS'
UNION ALL
SELECT 1 as intorder, u.id, q.order_type
FROM test_union u, test_query q
WHERE u.id = q.id
AND u.type is NOT NULL
AND q.field1 = 'TESTING'
UNION ALL
SELECT 2 as intorder, u.id, q.order_type
FROM test_union u, test_query q
WHERE u.id = q.id
AND u.type is NULL
AND q.field1 = 'DEFAULTS'
ORDER BY intorder, q.order_type;
【问题讨论】:
-
你没有。 JPQL 中没有这样的东西。
-
那么在 jpa 中有没有办法做到这一点?
-
JPA 允许您使用 SQL 查询,然后您抛弃了数据库独立性。
-
HQL 可以吗?
-
不知道。 JPA 不做“HQL”