This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with CascadeTypes

 
Gerard Kelly
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm still getting used to Hibernate but am currently having a little bother with deleting an object in a many-to-many relationship.

The functionality I have between the two models is pretty basic. Lets just say the two models are Product and Category, so many products can have many categories and vice versa.

If I delete one product, I do not wish to delete the categories it references and vice versa. The two tables use an association table since its a many-to-many relationship.

I've tried several different CascadeTypes and none seem to work correctly.

Using cascadeType.MERGE produces the following error:

Cannot delete or update a parent row: a foreign key constraint fails

CascadeType.ALL deletes more than 1 row incorrectly (ie. a record in the same table if they share a common attribute)

Using NO cascadeType returns the following error:

org.hibernate.ObjectDeletedException: deleted entity passed to persist manyToMany

and CascadeType.PERSIST throws an exception also:

org.hibernate.TransientObjectException: object references an unsaved transient instance

Then if I specify the following on BOTH models:

@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "products")

I get an error also:

java.sql.BatchUpdateException: Cannot delete or update a parent row: a foreign key constraint fails (`lportal/ten_category_product`, CONSTRAINT `FKEF22751352CAE5BF` FOREIGN KEY (`product_fk`) REFERENCES `ten_product` (`id`))

Sorry for the long-winded explanation but if anyone could help with this, I'd more than appreciate it!

Thanks.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps, it 'll be a good idea if you could post your HBMs and the POJOs and the code calling the save.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic