Hibernate. 2 functions : simpleSave() , saveWithCascade() . can it be done ? and how?
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/persi开发者_开发知识库sts 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
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
Parent p = new Parent();
Child c = new Child();
p.addChild(c);
session.save(p);
session.flush();
session.save(c);
session.flush();
Important thing is to ensure that the child drives the relationship. You can do that by setting inverse = "true"
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.
精彩评论