|
I have read the book, the documentation and FAQ. Still not able to figure this one out pls help.
I have two entity classes User and Patient.
...
@OneToMany(mappedBy = "user", cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
@org.hibernate.annotations.Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
private Set<Patient> patients = new HashSet<Patient>();
...
In the controller layer I have this delete method which uses an id from view layer to load a Patient for deletion.
public void delete()
Patient patient = patientDAO .findById(getDelete_id(), true);
//chase pointers and remove referrences from Users
List<User> listOfUsers = userDAO.chasePointersToPatient(patient);
logger.debug("size of set: "+ listOfUsers.size());//initalize collection
for(User u : listOfUsers) {
for(Patient p : u.getPatients()) {
if(p == patient) {
logger.debug("contains patient p: "+u.getPatients().contains(patient)+ "; size of set: "+ u.getPatients().size()); //contains patient p false; size of set 1
u.getPatients().remove(patient);
logger.debug("contains patient p: "+u.getPatients().contains(patient)+ "; size of set: "+ u.getPatients().size()); //contains patient p false; size of set 1
}
}
}
}
When I examine the u.getPatients() Set in debugger, i can see the exact same patient object, with same DB id, but some how u.getPatients.contains(patient) returns false? why?
p.s the code for userDAO.chasePointersToPatient is:
public List<User> chasePointersToPatient(Patient patient){
return (getSession()
.createQuery("from User u where :patient in elements(u.patients)")
.setParameter("patient", patient)
.list());
}
|