- Relationship mappings have a
cascade
option that allows the relationship to be cascaded for common operations. cascade
is normally used to model dependent relationships, such asOrder
->OrderLine
.- Cascading the
orderLines
relationship allows for theOrder
's ->OrderLine
s to be persisted, removed, merged along with their parent.
CascadeType enum:
PERSIST
- Cascaded theEntityManager.persist()
operation. Ifpersist()
is called on the parent, and the child is also new, it will also be persisted. If it is existing, nothing will occur, although callingpersist()
on an existing object will still cascade the persist operation to its dependents. If you persist an object, and it is related to a new object, and the relationship does not cascade persist, then an exception will occur. This may require that you first call persist on the related object before relating it to the parent. General it may seem odd, or be desirable to always cascade the persist operation, if a new object is related to another object, then it should probably be persisted. There is most likely not a major issue with always cascading persist on every relationship, although it may have an impact on performance. Calling persist on a related object is not required, on commit any related object whose relationship is cascade persist will automatically be persisted. The advantage of calling persist up front is that any generated ids will (unless using identity) be assigned, and theprePersist
event will be raised.REMOVE
- Cascaded theEntityManager.remove()
operation. Ifremove()
is called on the parent then the child will also be removed. This should only be used for dependent relationships. Note that only theremove()
operation is cascaded, if you remove a dependent object from aOneToMany
collection it will not be deleted, JPA requires that you explicitly callremove()
on it. Some JPA providers may support an option to have objects removed from dependent collection deleted, JPA 2.0 also defines an option for this.MERGE
- Cascaded theEntityManager.merge()
operation. Ifmerge()
is called on the parent, then the child will also be merged. This should normally be used for dependent relationships. Note that this only effects the cascading of the merge, the relationship reference itself will always be merged. This can be a major issue if you usetransient
variables to limit serialization, you may need to manually merge, or reset transient relationships in this case. Some JPA providers provide additionalmerge
operations.REFRESH
- Cascaded theEntityManager.refresh()
operation. Ifrefresh()
is called on the parent then the child will also be refreshed. This should normally be used for dependent relationships. Be careful enabling this for all relationships, as it could cause changes made to other objects to be reset.ALL
- Cascaded all the above operations.
Example of a cascaded one to one relationship annotations
@Entity
public class Employee {
@Id
private long id;
...
@OneToOne(cascade={CascadeType.ALL})
@JoinColumn(name="ADDR_ID")
private Address address;
...
}
No comments:
Post a Comment