之前其实就有遇到这个问题。我是在多的一方先删除掉数据 。再进行one端的update的。不然数据库里就会产生脏数据。例如
teacher.getStudentSet().clear();
Set newStudentSet = new HashSet();
//将新学生加入newStudentSet中
teacher.setStudentSet(newStudentSet);
dao.update(teacher);
执行以上的方法 会产生如下效果。
students端新插入了数据。但是之前插入的记录的外键变为空,而没有被删除掉。
一直认为有很好的其他办法解决。但是就没去尝试。今天查了下。有个方法是这样的。但是不确定这是好方法。
在one实体中增加如下方法
public void addStudentSet(Student s) {
getStudentSet().add(s);
s.setPerson(this);
}
设置cascade="all-delete-orphan"
在方法中
DAO dao= (DAO) BeanFactory.getBean("dao");
Teacher teacher = dao.getTeacherById(teacherId);
teacher.getStudentSet().clear();
//将新学生加入newStudentSet中
teacher.addStudentSet(student1);
teacher.addStudentSet(student2);
teacher.addStudentSet(student3);
dao.update(teacher);
据说这样就可以避免脏数据的产生。不知道这样是否是正确合理的方法。有更好的方法还请大家指导下啊。
分享到:
相关推荐
深入理解hibernate many-to-one(多对一)及 cascade(级联).
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联...
7.2.5. 一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 三重关联...
i. Many-to-one 1. false 2. proxy 3. no-proxy ii. set 一对多 1. true 2. false 3. extra 根据对set容器的不同,可以产生高效的sql访问数据库 2. 批量检索:batch-size=3 a) 可以使用批量检索: b) 在内存...
一对多关联(One-to-many Associations) 7.3. 高级集合映射(Advanced collection mappings) 7.3.1. 有序集合(Sorted collections) 7.3.2. 双向关联(Bidirectional associations) 7.3.3. 三重关联...
22.2. 双向的一对多关系(Bidirectional one-to-many) 22.3. 级联生命周期(Cascading lifecycle) 22.4. 级联与未保存值(Cascades and unsaved-value) 22.5. 结论 23. 示例:Weblog 应用程序 23.1. 持久化类 ...
一对多关联(One-To-Many Associations) 14 延迟初始化(延迟加载)(Lazy Initialization) 14 集合排序(Sorted Collections) 14 使用 <idbag> 14 双向关联(Bidirectional Associations) 14 三重关联(Ternary ...
6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,...
一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations)...
7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子关系 164 7.3 映射一对多双向自身...
7.1.1 [many-to-one]元素的not-null属性 153 7.1.2 级联保存和更新 155 7.2 映射一对多双向关联关系 156 7.2.1 [set]元素的inverse属性 161 7.2.2 级联删除 163 7.2.3 父子关系 164 7.3 映射一对多双向自身...
一对多关联(One-To-Many Associations) 6.5. 延迟初始化(延迟加载)(Lazy Initialization) 6.6. 集合排序(Sorted Collections) 6.7. 使用<idbag><br>6.8. 双向关联(Bidirectional Associations)...