1

我的DELETE查询失败。

这是我的代码:

String q = "SELECT p.id FROM Person p, DomainGroup g WHERE 
   p member of g.coordinators" + " AND g.id = :groupId 
   AND p.id = :personId";

List<Long> test = getEntityManager()
  .createQuery(q).setParameter("groupId", followingId)
  .setParameter("personId", followerId).getResultList();

log.debug("test = " + test);

String deleteGroupCoordinatorQuery = 
  "DELETE FROM Person p, DomainGroup g WHERE p member 
     of g.coordinators" + " AND g.id = :groupId 
        AND p.id = :personId";

List<Long> test = getEntityManager()
      .createQuery(deleteGroupCoordinatorQuery).setParameter
      ("groupId", followingId)
      .setParameter("personId", followerId).executeUpdate();

输出:

test = [1,2,3]

2012-10-26 13:44:56,437 ERROR org.company.commons.server.service.ServiceActionController - Error occurred performing transaction. java.lang.IllegalArgumentException: node to traverse cannot be null!
        at org.hibernate.hql.ast.util.NodeTraverser.traverseDepthFirst(NodeTraverser.java:55)
        at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:280)
        at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
        at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
4

2 回答 2

6

您定义了多个“查询根”(Person pDomainGroup g),但一个 DELETE 查询只能有一个这样的根。

你想要这样的东西:

delete Person p
where p.id in (
    select c.id
    from DomainGroup g
        join g.coordinators c
    where g.id = :groupId
      and c.id = :personId
)

或者

delete Person p
where p.id = :personId
  and p.id in (
    select c.id
    from DomainGroup g
        join g.coordinators c
    where g.id = :groupId
)
于 2012-10-26T15:45:37.703 回答
0

如果您不使用on delete cascade功能,那么您应该在删除之前删除对该对象的所有引用。

于 2012-10-26T16:27:18.213 回答