cascade用於級聯操作當前對象關聯的對象
inverse="false"用於在操作當前對象時,將與其關聯對象的關聯關係見起來。
在多對多關係中的直白說法就是:
cascade用於在保存a時,將b也相應保存了
inverse用於在保存a時,將中間表的對應關係也維護了。
比如:student 跟 teacher是多對多的關係,他們的關心中間表爲stu_teacher
那麼在student的配置文件中做如下配置的話
- <bag name="teachers" table="stu_teach" cascade="all" inverse="false">
- <key column="stuID" />
- <many-to-many class="com.test.Teacher"
- column="teahID" />
- </bag>
表示,cascade保證了在修改刪除Students實體時,也會相應的級聯操作Teacher,比如新增了一個Student,並新增一個Teacher對象,將其設置爲Student的teah屬性上。那麼在保存Student對象時,Teacher對象也會被保存到數據中。但中間表的關係並沒有值。只有inverse="false"這句,會促使在保存Student時,將中間表的關係也維護起來。
cascade儘量只用在one-to-one或者one-to-many的映射關係裏,詳細原因可查看hibernate reference guide中“傳播性持久化(transitive persistence)”中的講解。