How Hibernate / NHibernate Cascade attribute work

Posted by Zafar Ullah - zafarjcp@gmail.com | 12:57 AM | , , , , , | 0 comments »

Concept 1: when ever you have a set/bag/collection of child in parent class then its attributes must be inverse = true and cascade = "all-delete-orphan".

Concept 2: In above given senario In child class the reference of ParentID should be NotNull=true and if child creates a manytoone relation with parent then its cascade=none because on child deletion or update parent should not be updated.

Concept 3: If parent have no set/bag/collection of child then in child the parent will be NotNull=false means that it can be null. This senario is required if you mark foreignkeys in child table as allow null = true. where as if you mark foreignkeys as Not null in database then its property should be NotNull=true so that child record should not be saved if its parent's foreign key contains null.

Concept 4: if your foreignkey column in database allow null and due to any constraint you dont want to change db columns and you still want that Hibernate/Nhibernate will not save the record if user didnt provide its value then 2 things need to be done.

  • Validate from frontend that the foreign key value must be given
  • In your mapping file in child class mark parent field as NotNull=true. This will enforce that value should be given to this column before save/update.

    Please comment if i am wrong

0 comments