1.

@Entity  @Table(name = "Employee")  public class EmployeeEntity implements Serializable  {  private static final long serialVersionUID = -1798070786993154676L;  @Id  @Column(name = "ID", unique = true, nullable = false)  privateInteger           employeeId;  @Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)  private String            firstName;  @Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)  private String            lastName;  @OneToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY) `(name="EMPLOYEE_ID")  private Set<AccountEntity> accounts;  //Getters and Setters

Answer»

One of the main aspects of JPA is that it helps to propagate PARENT to Child RELATIONSHIPS. This behavior is possible through CascadeTypes. The CascadeTypes supported by JPA are:  

  1. CascadeType.PERSIST : This cascade TYPE means that save() or persist() operations cascade from the parent or owning entity to related entities. 
  2. CascadeType.MERGE :This cascade type means that related entities are merged when the parent or owning entity is merged. 
  3. CascadeType.REFRESH : This cascade type refreshes the related entities when the owning or parent entity calls the refresh() operation. 
  4. CascadeType.REMOVE : This cascade types removes all related entities associations when the owning entity is deleted. 
  5. CascadeType.DETACH : This cascade type detaches all related entities if a “manual detach” occurs. 
  6. CascadeType.ALL : When all the cascade operations have to be applied to the owning or parent entity, the cascade type all is used. 

The cascade configuration OPTION accepts an array of CascadeTypes. The below example shows how to add the refresh and merge operations in the cascade operations for a One-to-Many relationship: 

@OneToMany(cascade={CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.LAZY)  @JoinColumn(name="EMPLOYEE_ID"private Set<AccountEntity> accounts;

Cascading is USEFUL only for Parent - Child associations where the parent entity state transitions cascade to the child entity.  



Discussion

No Comment Found