OpenJPA/MySQL:修改一个table,同时在where子句中使用相同的table

OpenJPA/MySQL: modifying a table while the same table is used in the where clause

我想通过 openJPA 和 mysql 执行删除查询。

这个 mysql 语句工作正常:

delete from GENERIC 
 where PARENT_ID not in (select g2.ID from (select * from GENERIC) g2);

基本元素是具有列 ID 和 PARENT_ID

的 table GENERIC

将 GENERIC table 映射到 GenericEntity class,将 ID 列映射到(那个 class 的)id 成员,并将 PARENT_ID 列映射到 parentId 成员,我尝试了这个简单的方法测试:

entityManager.createQuery("delete from GenericEntity g1 where " +
   "g1.parentId not in " +
   "(select g2.id from (select * from GenericEntity) g2)"
).executeUpdate();

我得到这个错误:

org.apache.openjpa.persistence.ArgumentException: "Encountered "g1 . parentId not in ( select g2 . id from (" at character 36, but expected: ["(", "*", "+", ",", "-", ".", "/", ":", "<", "<=", "<>", "=", ">", ">=", "?", "ABS", "ALL", "AND", "ANY", "AS", "ASC", "AVG", "BETWEEN", "BOTH", "BY", "CASE", "CLASS", "COALESCE", "CONCAT", "COUNT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "DELETE", "DESC", "DISTINCT", "ELSE", "EMPTY", "END", "ENTRY", "ESCAPE", "EXISTS", "FETCH", "FROM", "GROUP", "HAVING", "IN", "INDEX", "INNER", "IS", "JOIN", "KEY", "LEADING", "LEFT", "LENGTH", "LIKE", "LOCATE", "LOWER", "MAX", "MEMBER", "MIN", "MOD", "NEW", "NOT", "NULL", "NULLIF", "OBJECT", "OF", "OR", "ORDER", "OUTER", "SELECT", "SET", "SIZE", "SOME", "SQRT", "SUBSTRING", "SUM", "THEN", "TRAILING", "TRIM", "TYPE", "UPDATE", "UPPER", "VALUE", "WHEN", "WHERE", , , , , , , , , ]." while parsing JPQL "delete from GenericEntity g1 where g1.parentId not in (select g2.id from (select * from GenericEntity) g2)". See nested stack trace for original parse error.

我尝试了不同的变体,也用更新替换了删除(改为设置 'deleted' 标志),但是当这非常 table 用于 where 子句。

非常感谢提示,如何继续或 link 任何有用的 material。 非常感谢您!

经过大量研究,我找到了解决办法。将 select 与删除查询分开并将 ID 列表作为参数传递给删除语句可以正常工作:

List<Integer> idList = entityManager
    .createQuery("select g.id from GenericEntity g",Integer.class)
    .getResultList();
entityManager
    .createQuery("delete from GenericEntity g where g.parentId not in (:idList)")
    .setParameter("idList", idList)
    .executeUpdate();