[size=large]cascade:级联,可选值为:none,save-update,delete,all,all-delete-orphan
inverse: 反转,表示由哪方维护关系false为自己维护,可选值:true,false[/size]
以上配置了: 1,一对多关联;2,“一”方维护关系;3;级联操作并删除孤儿;
(inverse,cascade不能再“多”方的配置文件<many-to-one.../>中配置)
“一”方更新时放入了一个新的SET,执行saveOrUpdate后“多”方原SET中对象的外键被置空(因为关系由“一”方维护,如果inverse=true,关系由“多”方维护“一”方会关联原SET和新SET),但没有执行删除孤儿的操作( cascade="all-delete-orphan" 已经这样配置了),要删除孤儿的正确操作是: 得到跟新的对象,根据对象ID get(xx.class,id) ,将得到的老对象的关系解除,把新对象的各属性放入老对象,更新成功,并删除了孤儿。
inverse: 反转,表示由哪方维护关系false为自己维护,可选值:true,false[/size]
<set name="soxAuditConditions" inverse="false" cascade="all-delete-orphan" lazy="false">
<key>
<column name="CASE_OBJ_ID" precision="20" scale="0" />
</key>
<one-to-many class="com.zte.ict.soc.auditManage.domain.SoxAuditCondition" />
</set>
以上配置了: 1,一对多关联;2,“一”方维护关系;3;级联操作并删除孤儿;
(inverse,cascade不能再“多”方的配置文件<many-to-one.../>中配置)
“一”方更新时放入了一个新的SET,执行saveOrUpdate后“多”方原SET中对象的外键被置空(因为关系由“一”方维护,如果inverse=true,关系由“多”方维护“一”方会关联原SET和新SET),但没有执行删除孤儿的操作( cascade="all-delete-orphan" 已经这样配置了),要删除孤儿的正确操作是: 得到跟新的对象,根据对象ID get(xx.class,id) ,将得到的老对象的关系解除,把新对象的各属性放入老对象,更新成功,并删除了孤儿。
Object old_sac_obj = this.getHibernateTemplate().get(SoxAuditCase.class, soxAuditCase.getObjId());
if(old_sac_obj!=null){//解除关系 更新
SoxAuditCase old_sac = (SoxAuditCase) old_sac_obj;
Set<SoxAuditCondition> sacns = old_sac.getSoxAuditConditions();
Iterator<SoxAuditCondition> it = sacns.iterator();
//存放要解除关系的对象(即:分组条件)
Set<SoxAuditCondition> removeCollection = new HashSet<SoxAuditCondition>();
while(it.hasNext()){
SoxAuditCondition temp = it.next();
//old_sac.getSoxAuditConditions().remove(temp);
removeCollection.add(temp);
temp.setSoxAuditCase(null);
}
old_sac.getSoxAuditConditions().removeAll(removeCollection);
//更新过的值导入原对象(缓存中的)
old_sac.setCaseName(soxAuditCase.getCaseName());
old_sac.setCaseNo(soxAuditCase.getCaseNo());
old_sac.setDescription(soxAuditCase.getDescription());
old_sac.getSoxAuditConditions().addAll(soxAuditCase.getSoxAuditConditions());
this.updateSoxAuditCase(soxAuditCase);
}else{
this.getHibernateTemplate().saveOrUpdate(soxAuditCase);
}