Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate:many-to-many: hibernate deletes too much

 
Matthew Tripp
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So here's the deal,

I have a 3 tables, test, test2, and test_test2_join (the association table between test and test2)
hibernate is doing its thing and when I have objects from test and test2 that are related, it automatically puts a row in the association table. Its great. The problem is when I delete a row.

Here's a junit test that illustrates the problem:


So if I have the following rows in Test


The following rows in Test2




the above test will end up deleting the rows I have marked with asterisks. This is bad. Don't want this. I only want the rows inserted in the junit test to be deleted. What is the very stupid mistake I am making? Any help is appreciated.


Also here is the mapping file, if that is helpful



thanks in advance

trippy
 
Benjamin Winterberg
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at how Cascade works in Hibernate. You have marked the relation with cascade = all. This means that when you delete an entity all related entities will be deleted as well.
 
Matthew Tripp
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ben win wrote:Take a look at how Cascade works in Hibernate. You have marked the relation with cascade = all. This means that when you delete an entity all related entities will be deleted as well.


Well shux.

perfectly logical and obvious. I am relying too much on tools generating my configuration and too little on actual understanding.

Thank you for the guidance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic