• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

delete child rows using hibernate. (cascade?)

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!

Suppose I have many-to-many relationship between question and test. Then I add some questions to a test.
So when I try to delete one of that questions, I get:
Cannot delete or update a parent row: a foreign key constraint fails (`sgc/test_question`, CONSTRAINT `FK75EC6E24266D1387` FOREIGN KEY (`questions_id`) REFERENCES `question` (`id`))

Is there some annotation to do this?
Does someone knows how can I manage do delete the child rows before delete the question?
 
David Coelho
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leandro,

When you remove the Question, the Hibernate Session doesn't have knowledge of all entities (attached and detached) that has a reference to the removed Question.

You just has to remove the Question of Test manually.

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David!

well ... I still think that hibernate can do it for me xD. I have to learn how to use cascade annotation.
but meanwhile I did what you recomended and it is working.


override equals in question:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic