• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Update Set of a many-to-many relationship

 
Alan Morelli
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a many-to-many relationship with a compositeId class in the middle:

Contest (Gara) --> ContestAgency (GaraAgenzia) + ContestAgencyId(GaraAgenziaId) <-- Agency (Agenzia)

I have a webflow where i can EDIT a Contest. Through the webflow I have checkboxes to select / deselect which Agencies are involved in the Contest.

I have many many troubles when I update the Contest object: or it doesn't update the ContestAgency records in the db, or I get this error:

org.springframework.dao.DuplicateKeyException: a different object with
the same identifier value was already associated with the session:
[com.myApp.model.GaraAgenzia#com.mmyApp.model.GaraAgenziaId@49f];
nested exception is org.hibernate.NonUniqueObjectException: a
different object with the same identifier value was already associated
with the session:
[com.myApp.model.GaraAgenzia#com.myApp.model.GaraAgenziaId@49f]


I tried also with merge (instead of update) but nothing. In some tries, i could be able to add new records but I couldn't remove orphans (Agencies that have been unchecked)

I need your kind help to
1) check if Cascade and many-to-many relations are correct or if there are mistakes (It's my really first application with Java, and it's very hard for me)
2) how can I do this update
3) some directions on how i can implement the equals() and hashCode() methods, as I found on internet that maybe are the source of my troubles.
but or i get StackOverflow error, or DuplicateKeyException, or it doesn't update anything.
4) others workaround to this problem. I'm trying to solve it using pure SQL on db, but it's not working and also not an elegant solution: it works, but after update DAO method,
something remove all rows from db. I used debugger but i could't figure out which internal method is called... I would say something with *proxy* word, but i'm not sure

Thank you very much. Any tip or suggestion are welcome because I really don't know where to bang my head to solve it and... I already destroyed 5 keyboards ... :'(


Gare.java:




GaraAgenzia.java:



GaraAgenziaId.java:




Here my Update method with some tries:

 
Alan Morelli
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure i resolved.

GaraAgenzia class:



GaraAgenziaId class:




Then in my Gara class:



My UpdateGara DAO function:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic