Thursday, January 3, 2019

What is the difference between cascade and orphan removal from DB?

[ @OneToMany(cascade=REMOVE, mappedBy="customer")
public List<Order> getOrders() { ... }
and

@OneToMany(mappedBy="customer", orphanRemoval="true")
public List<Order> getOrders() { ... } ]
 
Solution: 
  CascadeType.REMOVE indicates that remove operations should be
  cascaded automatically to entity objects that are referenced by that
  field of other entity.  
@Entity
class Employee {
     :
    @OneToOne(cascade=CascadeType.REMOVE)
    private Address address;
     :
}
 
Orphan Removal
JPA 2 supports an additional and more aggressive remove cascading mode which can be specified using the orphanRemoval element of the @OneToOne and @OneToMany annotations:
 
@Entity class Employee {
   
 @OneToOne(orphanRemoval=true) 
 private Address address;  

 }
 

No comments:

Post a Comment

Spring Annotations