Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

hibernate - What is the difference between DELETE_ORPHAN and DELETE?

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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...