views:

27

answers:

2

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

A: 

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"

Kartik
What if its just a one way relationship from parent to child ?
Blitzkr1eg
You don't have to do anything. By default inverse = "false"
Kartik
A: 

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.

Pascal Thivent