posted 19 years ago
Hi,
I've run into a problem with hibernate relationships.
It only appears to occur when using a 1-to-many, bi-directional relationship with *cascade*.
Tried posting this question to the hibernate forum, but was bitterly disappointed by their attitude (admittedly they have a right to it, as they're not being payed).
Would any of you veteran hibernate users happen to find the time to try it out ? If so, i'd be forever in your debt...
-----------------------------------------
Problem in short: when using 1:M, bi-directional relationship with "cascade", hibernate saves the parent but fails to save children.
I shall demonstrate for relationship "Department has several Employees".
-----------------------------------------
The classes:
------------------------
-----------------------------------------
The mapping (bi-di relashionship, with "inverse=true" and "cascade="all":
------------------------
------------------------
Oracle tables:
------------------------
------------------------
Usage:
------------------------
------------------------
The problem:
------------------------
Department is saved to the DB, but child employees are not. According to the log, hibernate issues an sql "update" instead of "insert".
Interesting notes:
1) Problem only occurs with "cascade". When removing the cascade, you need to explicitly save each employee, but at least it works.
2) Behaviour differs if you save the new department before adding employees, namely:
session.save(dept); dept.addEmployee(emp); session.save(emp);
Again, any help would be greatly appreciated.