Why in “One to Many” Mapping hibernate data is getting inserted in child table while updating parent table ?

Solution :

You are using cascade = CascadeType.ALL in both @OneToMany mappings of your Sets.

This means that whatever operation done on the parent entity, will be propagate also to the child entity

That is when you

  • PERSIST
  • REFRESH
  • MERGE
  • REMOVE
  • DETACH

the same would be applied to these children.

If you only want to cascade when the parent entity is

  • PERSISTED (or saved in the hibernate language)
  • DELETED (two most common options for @OneToMany)

, but during MERGE and subsequent update, you do not want to cascade those operations to children then set-up you mappings as follows:

@OneToMany(fetch=FetchType.EAGER, mappedBy="billInformation"
          , cascade = { CascadeType.PERSIST, CascadeType.REMOVE }
          ,orphanRemoval=true)
Set<BillingItemInformation> setBillingItemInformation;

@OneToMany(fetch=FetchType.EAGER, mappedBy="billInformation"
         , cascade ={ CascadeType.PERSIST, CascadeType.REMOVE }
         ,orphanRemoval=true)
Set<BillingTaxInformation> setBillingTaxInformation;

Update

With hibernate annotation you would configure as follows:

@Cascade({CascadeType.PERSIST, CascadeType.DELETE})

Laisser un commentaire