使用join或嵌套子查询Hibernate(HQL)进行更新

时间:2014-04-01 13:19:08

标签: java hibernate hql

我在创建使用嵌套查询或使用连接更新实体委员会成员的HQL时出现问题,我首先尝试了此查询:

update CommitteeMemberShip cms set cms.isDeleted=1 
where cms.committeeCycle.committee.id=:committeeId

但生成的SQL错误如下:

update CommitteeMemberShip cross join  set isDeleted=1 where committee_id=?

在“交叉连接”之后没有任何这个使得Hibernate抛出SQLGrammarException

之后,我更改了查询以使用子查询:

update CommitteeMemberShip cms set cms.isDeleted=1 
where cms.id in (select cmsIn.id from CommitteeMemberShip cmsIn inner join 
cmsIn.committeeCycle cc inner join cc.committee c where c.id=:committeeId)

现在Hibernate抛出

java.sql.SQLException: You can't specify target table 'CommitteeMemberShip' for 
update in FROM clause

任何人都知道如何在HQL中编写此更新查询?

2 个答案:

答案 0 :(得分:0)

试试这个

更新CommissionMemberShip cms set cms.isDeleted = 1 其中cms.id = some(从委员会成员cmberIn内部联接中选择cmsIn.id cmsIn.committeeCycle cc inner join cc.committee c其中c.id =:committeeId)

答案 1 :(得分:0)

试试这个 " UPDATE table1 r SET r.column = @value WHERE r.column IN(" SELECT rd.Id FROM r.table2 rd WHERE rd.Column ='" + @ value + "' AND rd.Column ='" +值+"'";