I'm having a database with a parent-child-like structure. At some point I collect a list (using bag) of parents, and all parents have a list of children (also using bags) respectively. Also worth noting is that I keep the session open, in order to use lazy loading.
It is now possible for my users (it is a windows forms application) to add a child to a parent. The user can then edit the child, or save the child (plus some more actions...). The user can also delete the newly created child, i.e. delete the child from the parents list of children, before it is saved in the database. It is here I run into a major problem.
It seems like Hibernate has private version of the newly created child object, and when I delete the parents version of the child, the Hibernate version remains alive. If a add a new child to the parent and save it, Hibernate throws an exception when it tries to first delete the “old” child object, the object that was never meant to be saved.
I have tried to delete the old child object from the secondary cache (using the evict method), but since it has never been saved to, or loaded from, the database, the object is not located in the cache.
Does anyone have an idea of how I can get rid of the object?
Or is this simply how Hibernate works?
I would guess that if I close and reopen the session this problem would bot exist, but that is not an options for me. The session must remain open.
|