Hibernate、HSQL 和更新(带限制)

发布于 2024-09-18 07:04:31 字数 496 浏览 2 评论 0原文

是否可以限制使用 Hibernate/HQL 更新的行数?例如:

Query q = em.createQuery("UPDATE MyObj o Set o.prop = :prop");
q.setParameter("prop", "foo");
q.setMaxResults(myLimit);

int res = q.executeUpdate();
if (res > myLimit) {
    // This is entering here and I don't want it to!
}

我一直在谷歌搜索等,我正在尝试使用 HQL,以便我可以在内存数据库中使用 HSQL DB 以及在部署中使用 MySql 进行一些单元测试。 MySql 支持 Update 语句上的 Limit 子句,但 HSQL 不支持,并且在 HSQL 中使用内部 select 执行 UPDATE 需要 order by,这似乎是一个坏主意。有没有办法实现限制更新中的行数?

谢谢。

Is it possible to limit the number of rows that are updated using Hibernate/HQL? For instance:

Query q = em.createQuery("UPDATE MyObj o Set o.prop = :prop");
q.setParameter("prop", "foo");
q.setMaxResults(myLimit);

int res = q.executeUpdate();
if (res > myLimit) {
    // This is entering here and I don't want it to!
}

I've been Googling around and such, and I am trying to use HQL so that I can do some unit tests using HSQL DB in memory dbs, as well as using MySql in deployment. MySql supports the Limit clause on Update statements, but HSQL does not, and doing an UPDATE with an inner select in HSQL required an order by, which seemed like a bad idea. Is there a way for me to achieve limiting the number of rows in an update?

Thanks.

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

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

发布评论

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

评论(1

花期渐远 2024-09-25 07:04:31

尝试 HSQLDB 2.0(最新的快照 jar,而不是 GA)与 Hibernate 3.5.5

它确实需要最后带有 LIMIT 的内部选择,但这不再需要 ORDER BY。此外,如果 ORDER BY 与 SELECT 使用的索引相同,则 ORDER BY 可以使用索引。

一个例子是这样的:

UPDATE MyObj o SET o.prop = :prop WHERE o.id IN (SELECT id FROM MyObj LIMIT 10)

Try HSQLDB 2.0 (the latest snapshot jars, not the GA) with Hibernate 3.5.5

It does require an inner select with LIMIT at the end but this no longer requires an ORDER BY. Also, ORDER BY can use an index if it is the same index as the one used for SELECT.

An example would be like:

UPDATE MyObj o SET o.prop = :prop WHERE o.id IN (SELECT id FROM MyObj LIMIT 10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文