This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes hibernate:many-to-many: hibernate deletes too much Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate:many-to-many: hibernate deletes too much" Watch "hibernate:many-to-many: hibernate deletes too much" New topic
Author

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

Matthew Tripp
Greenhorn

Joined: Jul 15, 2009
Posts: 5
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


I hate signatures.
Benjamin Winterberg
Ranch Hand

Joined: Sep 19, 2007
Posts: 36
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.


Hibernate DAOs with Spring | Separation of Concerns | Eclipse Code Templates | more...
Matthew Tripp
Greenhorn

Joined: Jul 15, 2009
Posts: 5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate:many-to-many: hibernate deletes too much
 
Similar Threads
How to insert using hibernate?
Foreign Key Constraint Fails
hibernate:many-to-many the association table is not getting populated
Many to Many Mapping: Link table not getting updated
An association from the table "dependent" refers to an unmapped class: "Independent"