Hibernate - how to set null value so that cascading worked [interesting!]?
I'm talking about a detached object, that later I want to update with saveOrUpdate().
How can we at the same time set null and clear() a referenced collection?
A [1:1] B [1:M] C
B to C reference can be null, that means NO C's records for B.
now I want to be able at the same time set B to null so that all C records were del开发者_JAVA百科eted from a database. And then saveOrUpdate A !
Cascading is set to hibernate all, including delete orphan.
I can B.clear(), it will delete all records from C.
I can set B to null, that will set B to null, but C records WILL NOT BE DELETED ! (what a pity! not convenient indeed!)
how then?
Situation is weird, in reality.
I see the workaround in 2 transactions. First trans: clear a collection. Commit transaction.
Now all C records are deleted.
Second transaction: A.setB(null). Commit. That will set a field to NULL.
But how to do it in one step?
One can use
nullable=trueattribute of Join annotation or a similar thing in xml
精彩评论