冬眠。 2 个函数: simpleSave() 、 saveWithCascade() 。可以吗?又如何呢?
我有一个场景: 父类 Parent 具有一些简单的属性(int、String 等)和 2 组子类。 设置childrenA; 设置childrenB;
我可以为父 simpleSave(Parent p) 创建一个保存函数,该函数将仅保存/保留数据库中的父属性,并有一个函数 saveWithCascade(Parent p) 会级联到其子级吗?
稍后编辑:“保存”实际上是指“更新”。因为如果我知道只有一个属性或一组发生了变化,我不希望休眠对其他属性进行大量选择只是为了检查它们是否发生了变化
I have a scenario in which i have:
A parent class Parent which has some simple properties (int, String, etc) and 2 set of children.
Set childrenA;
Set childrenB;
Could i make a save function for parent simpleSave(Parent p) that will save/persists only the parent properties in the database and have a function saveWithCascade(Parent p) that will cascade to its children ?
later edit: by Save i actually mean Update. Because if i know that only a property or a set changed, i dont want hibernate to do lots of selects on the others just to check if they changed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以。假设您正确定义了休眠映射。您可以调用
session.save(Parent)
当您保存子项时,您可以在您的方法中执行类似的操作,
重要的是确保子项驱动关系。您可以通过设置
inverse = "true"
来做到这一点Yes you can. Assuming that you have your hibernate mappings defined appropriately. You can call
session.save(Parent)
When you are saving a child, you can do some thing like this in your method
Important thing is to ensure that the child drives the relationship. You can do that by setting
inverse = "true"
如果您将 Hibernate 配置为级联给定关联的特定操作,则无法禁用它。因此,实现您想要的唯一目的就是不级联 PERSIST 和 MERGE,并在
saveWithCascade()
方法中手动完成所有操作。If you configure Hibernate to cascade a particular operation for a given association, you can't disable it. So the only want to achieve what you want would be to not cascade PERSIST and MERGE and to do everything manually in your
saveWithCascade()
method.