Mapping Cascade in hibernate

The keyword ‘cascade’ refers to what happens when a parent object has child objects.In Hibernate the cascade option is used for defining the Owner in the relationship between the associated objects. It is an optional attribute that may be used to specify that which operation should be cascaded. Cascade has the various options "all ", " save | update | delete | merge " that may be used with either of the single option or may be used with the multiple options. Cascade decides the same operation done on the parent object is done on the associated object at the same time. A keyword "cascade" is generally used when there is a mapping does for the collection to handle the state of collection automatically.

none : do nothing. Just save the object we’re saving and ignore the children
save-update : when updating the parent, save the children too.
delete : when deleting the parent, its children are automatically saved
all : all actions are cascaded from the parent to the child
all-delete-orphan : all actions are cascaded from the parent to the child and also orphan children are deleted.

Cascade in *.hbm.xml :

<set name="stockDailyRecords" cascade="save-update" table="stock_daily_record"...>
            <column name="STOCK_ID" not-null="true" />
      <one-to-many class="StockDailyRecord" />

Cascade using Annotation :
Cascade is especially useful for SAVE_UPDATE (which is the operation cascaded at flush time if you use plain Hibernate Annotations - Hibernate EntityManager cascade PERSIST at flush time as per the specification).

@OneToMany( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
public Collection<Employer> getEmployers()


Recent Posts