Hibernate version: 2.1.3
Hi,
I am having a scenario where i retrieve a persistent object in a stateless session bean. I convert this persistent object to a domain element and pass it over to the web-tier.
The persistent object has a one-to-many relationship with one more persistent object, as below:
class A {
Object id;
....
java.util.Set childObjects;
}
class B {
Object id;
....
A parentObj;
}
The hbms have the appropriate relationship mappings. I am having a cascade all-delete-orphan strategy. So whenever i remove a entry from the set of childObjects the corresponding entry in the database is deleted.
The problem i am facing is:
- I convert the persistent object into a domain element
- The original Set in the parent persistent object is converted to a set of domain elements. This implies that i lose the reference to the original set.
- At the web-tier i remove one(or more) elements from the set of child objects.
- Then i pass this object to the session bean for persisting it back
- I then convert this domain element back into a persistent object.
- I then call a update on the persistent object(Note: I am using <timestamp> tag in my hbms>
- Ideally, hibernate should delete those entries from database which were removed from the set. However, hibernate is not firing any delete query.
I found the following piece of information on some site:
#########################################################
Unlike other Hibernate value types, Hibernate tracks actual collection instances using Java identity, ==. Your getter method should return the same collection instance as was assigned by Hibernate to the setter method
#########################################################
So, i maintained the reference of the set of childObjects in my domain elements. I then removed an element from this set at the web-tier and passed the same set back to the bean as before. The bean then called a update on the parent object.
In this case the appropriate entries WERE DELETED from database.
(Note: I did not do any changes to hbm files )
I would not like to use this as a solution, since i have a very complex object graph. I went through the following link which talks about similar thing by using select-before-update:
http://www.hibernate.org/161.html
I followed this approach too but without success.
Can anyone suggest a solution to this problem?