Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between DELETE_ORPHAN and DELETE?

Tags:

hibernate

Here is source code:

@OneToOne(fetch = FetchType.LAZY)
@Cascade({SAVE_UPDATE, EVICT, DELETE})
@JoinColumn(name = "A_ID", nullable = true)
private A a;

@OneToMany
@Cascade({SAVE_UPDATE, EVICT, DELETE, DELETE_ORPHAN})
@JoinColumn(name = "B_ID")
private List<B> bList;

What is the difference between DELETE_ORPHAN and DELETE ?

like image 476
Forrest Avatar asked Sep 04 '09 06:09

Forrest


1 Answers

Cascade DELETE means if this entity is deleted, delete the related entity or entities.

DELETE_ORPHAN means if an entity is removed from a related one-to-many collection, then not only disassociate it from the current entity, but delete it.

To give you an example, consider two entities: House and Room.

DELETE on the Room list on House means that if you delete the House then delete all it's Rooms.

DELETE_ORPHAN on the Room list on House means if you remove a Room from that collection, delete it entirely. Without it, the Room would still exist but not be attached to anything (hence "orphan").

In UML and OO modelling terms, this is basically the difference between composition and aggregation. The House->Room relationship is an example of composition. A Room is part of a House and doesn't exist independently.

An example of aggregation is, say, Class (parent) to Student (child). Delete the Class and the Student still exists (undoubtedly in other classes). Removing the Student from the Class doesn't typically mean deleting him or her.

like image 175
cletus Avatar answered Nov 03 '22 22:11

cletus