Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Cascade deletion

 
Prabaharan Ramamoorthy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I have two entities. entity1 and entity 2 .Where entity2 has a many to one relation with the entity1.

entity 1:

property : id ----PK
property : code ----FK

entity 2 :

property : serial -pk
property : entity1-- (many to one reletaion with entity 1 fro code property)
property : module

but while deleting a record from entity 2 is trying to delete the respective object from entity 1.

I supposed to delete the row from entity 2 based on the code and serial.

expected query :

delete from entity2 where module = ? and entity1.code=?


the generated hql quesry :

delete from entity1,entity2 where module = ? and entity1.code=?

I dont know what went wrong.

Kindly help me!!!

Thanks In Advance .
 
Jana reddyMadam
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that's the correct behavior , because you want to delete the parent object Entity 2 and its primary key is referred in Entity 1 (So it will become Orphan in that case). So to make sure there are no Orphan Object hibernate is deleting child object when you delete parent object.

Note : you can change the Cascade configuration on you ManyToOne Annotation , so that it will not delete the child you intended.

look

@ManyToOne(cascade=CascadeType.ALL) // All means it whatever happens to parent ,child objects also get the same impact. you can change it to below

@ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH } // but remember Entity 2 primary should not be a foreign key any other table - Data base will not allow to delete this.

Could you please give the configuration you have, so that we can solve this. Let me know if you still want more info.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic