| Author |
need help with cascade delete
|
kwame Iwegbue
Ranch Hand
Joined: Sep 02, 2000
Posts: 197
|
|
I have read documentation and Gavin's hibernate book, but still have some problems with cascade delete (using cascade-all and delete-orphan) I have two entity classes User and Patient. In the controller layer I have this delete method which uses an id from view layer to load a Patient for deletion. When I examine the Set in debugger, i can see the exact same patient object, with same DB id, but some how calling "contains" on the Set returns false? why? p.s the code for userDAO.chasePointersToPatient is: [ June 26, 2007: Message edited by: kwame Iwegbue ] [ June 26, 2007: Message edited by: kwame Iwegbue ]
|
Who dares, wins! (SAS motto)
|
 |
Jaikiran Pai
Saloon Keeper
Joined: Jul 20, 2005
Posts: 6718
|
|
|
Can you post the contents of your Patient class. Also, are you overriding the equals and hashcode method in your Patient class?
|
[My Blog] [JavaRanch Journal]
|
 |
kwame Iwegbue
Ranch Hand
Joined: Sep 02, 2000
Posts: 197
|
|
Thanks for the quick reply Jaikiran, here is the code for my Patient class:
|
 |
Jaikiran Pai
Saloon Keeper
Joined: Jul 20, 2005
Posts: 6718
|
|
I havent yet looked at the entire code, but i believe the reason why the contains method is returning false, has to do with the logic present in the equals and the hashcode method:
Originally posted by kwame Iwegbue: public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Patient)) return false; final Patient patient = (Patient) o; return getDemographics() .getLastname() .equals(patient.getDemographics().getLastname()); } public int hashCode() { int result; result = (getDemographics() != null ? getDemographics().hashCode() : 0); result = 29 * result + (user != null ? user.hashCode() : 0); return result; }
Can you try printing out the hashcode values for each of the patient objects in
u.getPatients()
and also the hashcode of the patient that you are checking for:
Patient patient = patientDAO .findById(getDelete_id(), true);
This will help you to figure out whether the logic involved in the equals or the hashcode method is not right
|
 |
kwame Iwegbue
Ranch Hand
Joined: Sep 02, 2000
Posts: 197
|
|
seems you're right about the hashcode problem: log shows: I don;t understand how this can be? The set only contains 1 object "p", which it compares in the foreach and returns the same hashcode as "patient".
|
 |
kwame Iwegbue
Ranch Hand
Joined: Sep 02, 2000
Posts: 197
|
|
|
I removed my implementation of equals and hashcode, now the code works fine. I wonder why I had those in the first place?
|
 |
Jaikiran Pai
Saloon Keeper
Joined: Jul 20, 2005
Posts: 6718
|
|
Originally posted by kwame Iwegbue: I removed my implementation of equals and hashcode, now the code works fine.
Glad to know, it works
Originally posted by kwame Iwegbue: I wonder why I had those in the first place?
Overriding the eqauls and hashcode is perfectly fine, as long as they are implemented correct. If you are more curious about the best strategies for implementing equals and hashcode in your persistent classes, have a look at Equals and HashCode - Hibernate
|
 |
 |
|
|
subject: need help with cascade delete
|
|
|