SQL 代码在转换为 JPQL 代码时不起作用(ORA-00904 错误代码)
SQL code does not work when converted to JPQL code (ORA-00904 error code)
非常感谢您查看这篇文章 post :)。我有以下问题:我有一段 SQL 代码完美地 运行 在 Oracle 数据库上。当我尝试将 运行 作为 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 代码和来自 entityManager 的 createNativeQuery 方法解决了这个问题。似乎 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();
再次感谢阿贝纳莫的解决方案:)。
非常感谢您查看这篇文章 post :)。我有以下问题:我有一段 SQL 代码完美地 运行 在 Oracle 数据库上。当我尝试将 运行 作为 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 代码和来自 entityManager 的 createNativeQuery 方法解决了这个问题。似乎 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();
再次感谢阿贝纳莫的解决方案:)。