Normally used to <one-to-many> and <one-to-one>: "one" side
Not suggest for <many-to-one> and <many-to-many>: "many" side
- Can used in <Set> and also <Property>
- Used in <one-to-many>, "one" side
- Used in <one-to-one>, primary object side
Cascade options:
none
all
save-update:
delete
lock
refresh
evict
replicate
persist
merge
delete-orphan(one-to-many)
Examples:
cascade="all"
cascade="persist,merge,save-update"
cascade="delete"
Example:(one-to-many)
Auto delete students when delete a department.
in Department.hbm.xml
<set name="students" cascade="delete">
<key column="dept_id"/>
<one-to-many class="student" />
</set>
when doing delete a department, all students in its <set> will be deleted
Example:(one-to-many)
Auto add/update students when save a department.
in Department.hbm.xml
<set name="students" cascade="save-update">
<key column="dept_id"/>
<one-to-many class="student" />
</set>
When save a department, all students in its <set> will be saved/updated
Example:(many-to-one)
Auto add/update department when save a student
in Student.hbm.xml
<many-to-one name="department" column="dept_id" cascade="save-update">
<key column="dept_id"/>
<one-to-many class="student" />
</set>
When saving/updating a student, its department will also be saved/updated
No comments:
Post a Comment