OpenJPA / MySQL:在where子句

时间:2017-03-16 21:06:18

标签: jpql openjpa

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

这个mysql语句可以正常工作:

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

基本元素是具有列ID和PARENT_ID

的表GENERIC

将GENERIC表映射到GenericEntity类,将ID列映射到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:"遇到" g1。   parentId不在(选择g2.id from(" at character 36,但是。)   预期:["("," *"," +",","," - &# 34;,"。"," /",":","<","< =","<>",   " =",">","> =","?"," ABS&#34 ;," ALL"," AND"," ANY"," AS"," ASC",&#34 ; AVG&#34 ;,   " BETWEEN"," BOTH"," BY"," CASE"," CLASS"," COALESCE& #34;," CONCAT",   " COUNT"," CURRENT_DATE"," CURRENT_TIME"," CURRENT_TIMESTAMP",   " DELETE"," DESC"," DISTINCT"," ELSE"," EMPTY"," END& #34;," ENTRY",   " ESCAPE"," EXISTS"," FETCH"," FROM"," GROUP"," HAVING& #34;," IN"," INDEX",   " INNER"," IS"," JOIN"," KEY"," LEADING"," LEFT& #34;," LENGTH"," LIKE",   " LOCATE"," LOWER"," MAX","会员"," MIN"," MOD& #34;," NEW"," NOT",   " NULL"," NULLIF"," OBJECT"," OF"," OR"," ORDER& #34;," OUTER"," SELECT",   " SET"," SIZE"," SOME"," SQRT"," SUBSTRING"," SUM& #34;,"那么"," TRAILING",   " TRIM"," TYPE"," UPDATE"," UPPER"," VALUE"," WHEN& #34;," WHERE",   ,,,,   ,,,   ,]。"解析JPQL"删除   GenericEntity g1,其中g1.parentId不在(从中选择g2.id)   来自GenericEntity)g2)"。请参阅嵌套堆栈跟踪以获取原始解析   错误。

我尝试了不同的变体,也用更新替换了删除(以设置'已删除的'标志),但是当使用此表时,修改表似乎是一般问题where子句。

我非常感谢提示,如何继续或链接到任何有用的材料。 非常感谢你提前!

1 个答案:

答案 0 :(得分:0)

经过大量研究,我找到了解决方案。它可以将select与delete查询分开,并将ID列表作为参数传递给delete语句:

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();