gpt4 book ai didi

spring - 使用 JPA 2.0 和 @OneToMany 删除孤立实体的解决方法是什么?

转载 作者:行者123 更新时间:2023-12-03 20:37:43 25 4
gpt4 key购买 nike

我使用的是 JPA 2.0、Hibernate 4.1.0.Final、Spring 3.1.1.RELEASE 和 Java 1.6。我有这个实体与另一个实体有一对多的关系......

import javax.persistence.CascadeType;
...
@Entity
@Table(name = "classroom")
public class Classroom implements Serializable
{
...

@OneToMany(mappedBy = "classroom", cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
private Set<ClassroomUser> roster;

但是,当我使用一组不同的 ClassroomUser 对象更新我的实体时
classroom.setRoster(newRoster);

并保存实体,所有以前的 ClassroomUser 对象仍然存在。从数据库中删除所有孤立记录的同时更新我的​​实体的正确/最短方法是什么?

谢谢, - 戴夫

最佳答案

使用 orphanRemoval :

@OneToMany(mappedBy="classroom", cascade={CascadeType.ALL}, orphanRemoval=true)

每当从持久集中删除条目时,它将被删除。这意味着你需要工作 使用持久集 . IE。您不得更换该套装,而应执行以下操作:
classroom.getRoster().clear();
classroom.getRoster().addAll(newRoster);

示例 如何将持久集与用户所需的集同步:
/**
* Assemble ClassroomUser relations.
* @param classroom Classroom entity. Must be attached persistent or transient. Never null.
* @param userIds Collection of user identifiers. Can be empty. Never null.
*/
private void assembleClassroomUsers(Classroom classroom, Collection<Integer> userIds) {
// Make sure relation set exists (might be null for transient instance)
if (classroom.getUsers() == null) {
classroom.setUsers(new HashSet<ClassroomUser>());
}
// Create working copy of the collection
Collection<Integer> ids = new HashSet<Integer>(userIds);
// Check existing relations and retain or remove them as required
Iterator<ClassroomUser> it = classroom.getUsers().iterator();
while (it.hasNext()) {
Integer userId = it.next().getUser().getId();
if (!ids.remove(userId)) {
it.remove(); // This will be picked by the deleteOrphans=true
}
}
// Create new relations from the remaining set of identifiers
for (Integer userId : ids) {
ClassroomUser classroomUser = new ClassroomUser();
classroomUser.setClassroom(classroom);
// User must not have ClassroomUser relations initialized, otherwise Hibernate
// will get conflicting instructions what to persist and what to drop => error.
// It might be safer to use dummy transient instance...
User dummyUser = new User();
dummyUser.setId(userId);
classroomUser.setUser(dummyUser);
classroom.getUsers().add(classroomUser);
}
}

这种方法可能看起来有点复杂。您也许可以使用自定义 equals 创建更简单的东西(但可能不会太多)/ hashCode和一些 Set<E>操作方法(例如来自 Guava )。

关于spring - 使用 JPA 2.0 和 @OneToMany 删除孤立实体的解决方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17025927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com