Hibernate、HSQL 和更新(带限制)
是否可以限制使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试 HSQLDB 2.0(最新的快照 jar,而不是 GA)与 Hibernate 3.5.5
它确实需要最后带有 LIMIT 的内部选择,但这不再需要 ORDER BY。此外,如果 ORDER BY 与 SELECT 使用的索引相同,则 ORDER BY 可以使用索引。
一个例子是这样的:
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: