• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

need help with cascade delete

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the contents of your Patient class. Also, are you overriding the equals and hashcode method in your Patient class?
 
kwame Iwegbue
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick reply Jaikiran,

here is the code for my Patient class:

 
Jaikiran Pai
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 197
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic