SQL 代码在转换为 JPQL 代码时不起作用(ORA-00904 错误代码)
Posted
技术标签:
【中文标题】SQL 代码在转换为 JPQL 代码时不起作用(ORA-00904 错误代码)【英文标题】:SQL code does not work when converted to JPQL code (ORA-00904 error code) 【发布时间】:2021-05-10 22:14:02 【问题描述】:非常感谢您查看这篇文章 :)。我有以下问题:我有一段在 Oracle 数据库上完美运行的 SQL 代码。当我尝试以 JPQL 代码运行时,问题就来了。我在 Eclipse IDE 中收到以下错误:
内部异常:java.sql.SQLSyntaxErrorException:ORA-00904:“THIRDTABLE”。“NAME”:标识符无效。
有效的 SQL 如下:
SELECT e.status_q,
q.name,
q.masterdatastatus_id
FROM firstTable e
JOIN secondTable q ON e.status_q = q.id
AND q.masterdatastatus_id = e.status_s
WHERE q.id != 7122
AND e.qserie_id = 1296
AND q.masterdatastatus_id not in
(SELECT l1.status_s
FROM thirdTable l1
WHERE (l1.id,
l1.name) not in
(SELECT l2.id,
l2.name
FROM thirdTable l2
WHERE l2.status_s = 1))
AND
(SELECT count(*)
FROM thirdTable l3
WHERE l3.status_s = q.masterdatastatus_id) =
(SELECT count(*)
FROM thirdTable l4
WHERE l4.status_s = 1)
AND q.masterdatastatus_id in
(SELECT x.status_s
FROM thirdTable x
LEFT JOIN thirdTable y ON x.id = y.id
AND x.name = y.name
WHERE y.id IS NOT NULL
AND y.status_s = 1 )
ORDER BY q.id DESC;
这是不起作用的 JPQL 代码:
public static final String QUERY = "SELECT e.status, q.name, q.masterdatastatus.id FROM firstTableBE e JOIN secondTableBE q "
+ "ON e.status = q.id AND q.masterdatastatus.id = e.statusS WHERE "
+ "q.id != :STATUS_Q AND e.qSerieOid = :QSERIE_ID AND "
+ "q.masterdatastatus.id NOT IN (SELECT l1.status from ThirdTableBE l1 where (l1.oid, l1.name) NOT IN (SELECT l2.oid, l2.name from ThirdTableBE l2 where l2.status = 1)) AND "
+ "(SELECT COUNT(l3) from ThirdTableBE l3 where l3.status = q.masterdatastatus.id) = (SELECT COUNT(l4) from ThirdTableBE l4 where l4.status = 1) AND "
+ "q.masterdatastatus.id IN (SELECT x.status from ThirdTableBE x LEFT JOIN ThirdTableBE y on x.oid = y.oid and x.name = y.name where y.oid is not null and y.status = 1) "
+ "ORDER BY q.id DESC";
我猜问题出在这部分代码上:
"q.masterdatastatus.id NOT IN (SELECT l1.status from ThirdTableBE l1 where (l1.oid, l1.name) NOT IN (SELECT l2.oid, l2.name from ThirdTableBE l2 where l2.status = 1))"
JPQL 是否不支持子查询中的多个表达式?是'where'语句之后的那对吗?当我只尝试一个表达式(例如 l1.oid)时,它可以工作,但结果不是预期的。此外,如果我删除上面的代码,它可以工作,但同样,这不是我想要的结果。
提前感谢您的帮助! :)
【问题讨论】:
Sql 比 JPQL 更强大。如果无法将 sql 查询转换为 JPQL 查询,可以使用 entityManager 的 createNativeQuery 方法直接执行 sql 查询,而无需将其转换为 JPQL。 嗨阿贝纳莫。谢谢回复。我会试试你的建议,看看是否可行。 【参考方案1】:我通过使用 entityManager 中的 createNativeQuery 方法运行 SQL 代码解决了这个问题。 JPQL 似乎不太喜欢 where 语句中的对。也许我使用的 JPA 版本不支持对(我使用 JPA 2.6.4)。这是代码:
Query query = entityManager
.createNativeQuery("select e.status_q, q.name, q.masterdatastatus_id from firstTable e "
+ "join secondTable q on e.status_q = q.id and q.masterdatastatus_id = e.status_s "
+ " WHERE q.id != 7122 and e.qserie_id = 1296 and "
+ "q.masterdatastatus_id not in (select l1.status_s from thirdTable l1 where (l1.id, l1.name) not in (select l2.id, l2.name from thirdTable l2 where l2.status_s = 1)) and "
+ "(select count(*) from thirdTable l3 where l3.status_s = q.masterdatastatus_id) = (select count(*) from thirdTable l4 where l4.status_s = 1) and "
+ "q.masterdatastatus_id in (select x.status_s from thirdTable x left join thirdTable y on x.id = y.id and x.name = y.name where y.id is not null and y.staatus_s = 1 ) "
+ "order by q.id desc");
List<Object> result = query.getResultList();
再次感谢 Abenamor 的解决方案 :)。
【讨论】:
以上是关于SQL 代码在转换为 JPQL 代码时不起作用(ORA-00904 错误代码)的主要内容,如果未能解决你的问题,请参考以下文章
路由的默认 vue-cli 延迟加载代码在定义为 lambda 时不起作用