使用Mysql进行HQL更新的另一种方法

时间:2014-12-23 07:58:00

标签: mysql hibernate hql

我是HQL的新手,我需要在HQL中进行批量更新(没有mysql查询)。

以下是我的实体样本

EnityA{
    int id,
    Date date,
    int criteria1,
    EntityB b;
}

EntityB{
   int id,
   status,
   List<EntityA> aas;
}

现在我要更新所有具有criteria1 = 1且其对应的EntityB状态不等于6的所有As。

我只知道hql不支持隐式或显式连接,所以我想到了使用它们。然后我遇到了几个关于使用派生表的博客。所以我尝试了这个查询:

    update EntityA A set A.date= :someDate where A.id in 
(select temp.id from (select A1.id from EntityA A1 where A1.criteria=1 
and A1.b.status <> 6) as temp)

但不幸的是我有unexpected token: (例外。

我猜这是因为派生表也不受HQL支持。现在有办法吗?我真的需要检查EntityB的状态。

谢谢

1 个答案:

答案 0 :(得分:1)

你是对的,HQL for DML可以使用子查询,这些可以使用连接。如果你正确地观察你的查询,它可以像这样重写,这应该是有效的:

 update EntityA A set A.date= :someDate where A.id in 
 // (select temp.id from 
 (select A1.id from EntityA A1 where A1.criteria=1 and A1.b.status <> 6) 
 // as temp)

换句话说:

 update EntityA A set A.date= :someDate where A.id in 
 (select A1.id from EntityA A1 where A1.criteria=1 and A1.b.status <> 6) 

15.4. DML-style operations

  
      
  • ...
  •   
  • 不能在批量HQL查询中指定第16.4节“隐式或显式的连接语法形式”。 子查询可以在where子句中使用,子子查询本身可以包含连接。
  •