Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Problem with CascadeTypes

 
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.
 
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.
 
Get off me! Here, read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic