使用 set 子句中的子查询更新 Hibernate HQL

发布于 2024-09-10 14:47:49 字数 200 浏览 3 评论 0原文

我正在尝试在 hibernate HQL 中使用 set 子句中的子选择进行更新,例如:

update UserObject set code = (select n.code from SomeUserObject n where n.id = 1000)

它不起作用,不支持吗?

谢谢

乌多

I'm trying to do an update in hibernate HQL with a subselect in a set clause like:

update UserObject set code = (select n.code from SomeUserObject n where n.id = 1000)

It isnt working, it is not supported?

Thanks

Udo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2024-09-17 14:47:49

来自 Hibernate 文档:

13.4。 DML 风格的操作

...

UPDATE 的伪语法和
DELETE 语句是: ( UPDATE |
删除)从?实体名称(WHERE
where_conditions)?

一些注意事项:

  • 在 from 子句中,FROM 关键字是可选的
  • from 子句中只能有一个实体。它可以,
    但是,可以使用别名。如果实体
    名称是别名,然后是任何属性
    参考文献必须使用限定
    那个别名。如果实体名称不是
    别名,那么对于任何人来说都是非法的
    属性参考要合格。
  • 不能批量指定隐式或显式的连接
    HQL查询。 可以使用子查询
    在 where 子句中,其中
    子查询本身可能包含
    加入。
  • where 子句也是可选的。

虽然文档没有明确提及有关 set 部分的限制,但人们可以解释子查询仅在 where 子句中受支持。但是......

我发现了一个关于批量更新问题的4年前(叹息)问题(HHH-1658),据记者称,以下方法有效:

UPDATE Cat c SET c.weight = (SELECT SUM(f.amount) FROM Food f WHERE f.owner = c)

我想知道在 from 子句中使用别名是否有帮助。看来还是有一些奇怪的地方。

From the Hibernate documentation:

13.4. DML-style operations

...

The pseudo-syntax for UPDATE and
DELETE statements is: ( UPDATE |
DELETE ) FROM? EntityName (WHERE
where_conditions)?
.

Some points to note:

  • In the from-clause, the FROM keyword is optional
  • There can only be a single entity named in the from-clause. It can,
    however, be aliased. If the entity
    name is aliased, then any property
    references must be qualified using
    that alias. If the entity name is not
    aliased, then it is illegal for any
    property references to be qualified.
  • No joins, either implicit or explicit, can be specified in a bulk
    HQL query. Sub-queries can be used
    in the where-clause, where the
    subqueries themselves may contain
    joins.
  • The where-clause is also optional.

While the documentation doesn't explicitly mentions a restriction about the set part, one could interpret that sub-queries are only supported in the where-clause. But...

I found an 4 years old (sigh) issue about bulk update problems (HHH-1658) and according to the reporter, the following works:

UPDATE Cat c SET c.weight = (SELECT SUM(f.amount) FROM Food f WHERE f.owner = c)

I wonder if using an alias in the from-clause would help. Looks like there is some weirdness anyway.

秋日私语 2024-09-17 14:47:49

我遇到了同样的问题,发现您需要将批量更新放入事务中:

tr = session.getTransaction();
tr.begin();
updateQuery.executeUpdate();
tr.commit;

I had the same problem, discovered that you need to put bulk updates in side a transaction:

tr = session.getTransaction();
tr.begin();
updateQuery.executeUpdate();
tr.commit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文