Cascade in hibernate from the one to many side
i know this question might be asked before, but i want to make it specific,
am using hibernate without annotations, so my case is if I've the following relation:
A has many of B and B has one of A, it's a one to many relation from A side, am dealing with A entity which contain set of B, then when Creating, Updating Bs in runtime using A, then saving or updating A using hibernate, i want it also开发者_JAVA技巧 to save or update B i.e cascade save delete but from the side of A (one to many), i think it's allowed from B (many to one) side only
Regads,
I'm not sure I understood the question but it's definitely possible to define operations to cascade for a one-to-many association (see the section 6.2. Collection mappings). Below, an extract from the Chapter 21. Example: Parent/Child:
21.3. Cascading life cycle
You can address the frustrations of the explicit call to save() by using cascades.
<set name="children" inverse="true" cascade="all"> <key column="parent_id"/> <one-to-many class="Child"/> </set>
This simplifies the code above to:
Parent p = (Parent) session.load(Parent.class, pid); Child c = new Child(); p.addChild(c); session.flush();
Similarly, we do not need to iterate over the children when saving or deleting a
Parent
. The following removesp
and all its children from the database.Parent p = (Parent) session.load(Parent.class, pid); session.delete(p); session.flush();
However, the following code:
Parent p = (Parent) session.load(Parent.class, pid); Child c = (Child) p.getChildren().iterator().next(); p.getChildren().remove(c); c.setParent(null); session.flush();
will not remove
c
from the database. In this case, it will only remove the link top
and cause aNOT NULL
constraint violation. You need to explicitlydelete()
theChild
.Parent p = (Parent) session.load(Parent.class, pid); Child c = (Child) p.getChildren().iterator().next(); p.getChildren().remove(c); session.delete(c); session.flush();
In our case, a
Child
cannot exist without its parent. So if we remove aChild
from the collection, we do want it to be deleted. To do this, we must usecascade="all-delete-orphan"
.<set name="children" inverse="true" cascade="all-delete-orphan"> <key column="parent_id"/> <one-to-many class="Child"/> </set>
Even though the collection mapping specifies
inverse="true"
, cascades are still processed by iterating the collection elements. If you need an object be saved, deleted or updated by cascade, you must add it to the collection. It is not enough to simply callsetParent()
.
References
- 6.2. Collection mappings
- Chapter 21. Example: Parent/Child
Cascades work with unidirectional collections. Just add "cascade=all" or even cascade="all-delete-orphan" so that the child entity is deleted if it is removed from the collection.
精彩评论