This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In some tests with Hibernate, to remove an entity that appears in 3 lines of an intermediate table, instead of removing it to 3 lines in this entity, he removed and inserted again ALL lines referring to those who remained in the intermediate table.
To understand better, see the code:
Entity TEAM:Entity TEAMTYPE:Entity INDIVIDUAL:Hibernate generated the following SQL script:The database was empty, so TeamTypes, Teams and individuals were added:In order to obtain such a situation that attend this diagram:
Then, the intermediate table was something like this:The problem I found is to remove the individual P7 with ID 7. Here he is on 3 teams, and correctly displayed on 3 lines of intermediate table with the correct relationship with the 3 teams.
The code to remove it is this:In implementing this, Hibernate should do everything I need, besides deleting the row relating to the individual, it should only delete 3 lines in the intermediate table. Except that instead he:I have also implemented a trigger in the database (Oracle) and it inserted as well as deleted. I suspected that the INSERTS were informational only. It does delete individual correctly. But it's inserting again into the intermediate table. The most strange is that after this operation the intermediate table is correct and no exceptions are thrown.
I can't understand why Hibernate is doing this.
For a table with 12 records, this is "peanuts", but imagine if I had 2,000,000 and remove 3? it would reinsert 1,999,997?
not sure whether you need to remove the individual from all the teams....
I have not worked much on many to many mappings in hibernate, but i am wondering why a simple "session.remove(p7)" will not work.
Can you try it with "session.remove(P7)" and see if any exception comes...
To Brazil, with love...
Joined: Jul 31, 2009
Thanks for your reply!
I did it already! Look at the exceptions:
Joined: Jun 28, 2008
I think you can try to put the cascade="all" attribute for your association from Individual to the Team ...
Joined: Jul 31, 2009
I tried again with your suggestion and it did not work. Remains exactly the same problem.
Tested the various CASCADE options and there was no change in behavior.