休眠并删除所有 [英] hibernate and delete all
问题描述
在Hibernate中删除表中的所有行的最佳方法是什么?
如果我遍历集合并调用 session.delete()
它不符合我的要求。
如果我使用另一个选项 session .createQuery(delete ...)
它不影响持久化上下文。
当我不应该使用这些方法时更好的变体?
- 如果您没有任何级联,请使用HQL delete
删除enityName
- 如果您有级联,则迭代集合并单独删除每个集合。
ul> - if you don't have anything to cascade, use the HQL delete
DELETE FROM enityName
- if you have cascades, iterate the collection and delete each one individually.
问题在于hibernate在内部处理级联,而不是将其留给数据库。所以发送一个查询不会触发内部级联,因此你将有不一致/孤儿。
如果性能如此重要(毕竟不是每天都会截断一个表),那么每个级联可以有多个HQL删除 - 即手动处理级联。
What is the best way to delete all rows in a table in Hibernate?
If I iterate over a collection and call session.delete()
it's not performing to my knowledge.
If I use another option session.createQuery("delete ...")
it doesn't affect persistence context.
When I should use these methods if there is no better variant?
The problem lies in the fact that hibernate handles cascades internally, rather than leaving this to the database. So sending a query won't trigger the internal cascades, hence you will have inconsistencies / orphans.
If performance is so crucial (after all it's not everyday that one truncates a table), then you can have more than 1 HQL delete for each cascade - i.e. handling the cascades manually.
这篇关于休眠并删除所有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!