2012-10-26 46 views
1

我的DELETE查詢失敗。休眠錯誤IllegalArgumentException:要遍歷的節點不能爲空

這裏是我的代碼:

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) 
+0

它可能與級聯有關嗎? – pengibot

+0

@pengibot請你詳細說明一下嗎? –

+0

對不起,只是認爲這可能是它的原因。聽起來像史蒂夫的答案是你問題的根源。 – pengibot

回答

6

可以定義多個 「查詢根」(Person pDomainGroup g),但刪除查詢只能有一個這樣的根。

你想要的東西,如:

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 
) 
+0

應該工作嗎? 「DELETE FROM DomainGroup g WHERE g.id =:groupId AND:personId g.coordinators的成員」 –

+0

這將刪除DomainGroups。那是你要的嗎? –

+0

我不知道。我只想刪除Person p,DomainGroup g的id,其中p.id是g.coordinators的成員。 –

0

如果你不使用on delete cascade功能,那麼你應該將其刪除之前刪除的對象的所有引用。