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



我使用JPA 2.0, Hibernate 4.1.0。最后,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对象。什么是正确的/最短的方式来更新我的实体,同时从数据库中删除所有孤儿记录?

谢谢,Dave

使用orphanRemoval:

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

无论何时从持久集中删除一个条目,它都将被删除。这意味着您需要使用持久集来处理。也就是说,你不允许替换这个集合,你应该这样做:

classroom.getRoster().clear();
classroom.getRoster().addAll(newRoster);

EXAMPLE如何同步persistent set和user required set:

/**
 * 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)。

相关内容

  • 没有找到相关文章

最新更新