I am new to Hibernate, but I have read the tutorial and The 'King/Bauer' referrence book. I have an application built on Hibernate and it mostly works very well. I have no trouble managing Entity objects and associations of entities; I'm however having some difficulty managing a Collection of Value type objects. When I attempt to delete a Value type object form a Set that is mapped to an Entity, the object is initially removed from the Collection in the Controller, but when it returns to the View and the Entity is loaded from the database, it still contains the deleted object. I'm not sure what I could be doing wrong. I'm using Open Session in View pattern, so the request/response cycle is wrapped in a Filter.
Here is some code from the Entity object in my application:
Code:
@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true)
@Table(name = "VISIT")
public class Visit implements Serializable {
@Id @GeneratedValue
@Column(name="VISIT_ID")
private Long id;
...
@org.hibernate.annotations.CollectionOfElements
@JoinTable(name = "DIAGNOSES", joinColumns = @JoinColumn(name = "VISIT_ID"))
private Set<Diagnosis> diagnoses = new HashSet<Diagnosis>();
...
So Visit is an Entity (has database identity) and Diagnosis is a Value type (has no database identity).
I can manage a Visit object with
Code:
getSession().saveOrUpdate(visit);
getSession().delete(visit);
However, this won't work with Diagnosis. So if I want to delete a Diagnosis object, which is in a Set that is mapped to a Visit object, I do this:
Code:
...
Visit v = visitDAO.findById(getVisit_id(), true);
Set<Diagnosis> diagnoses = v.getDiagnoses();
for (Iterator iter = diagnoses.iterator(); iter.hasNext(); ) {
Diagnosis element = (Diagnosis) iter.next();
v.getDiagnoses().remove(element);
}
...
So this works, and removes the Diagnosis from the Collection, however, when Visit is loaded again, the "deleted" Diagnosis is still present in the Collection!
Someone please help!!!