管理JPA中双向关系的问题:从集合中删除

时间:2011-01-10 15:55:48

标签: java jpa eclipselink jpa-2.0

我的域模型具有自引用的双向关系,在实体中完成关系管理:

@Entity
public class Users implements BaseEntity<String>, Serializable {

    @Id
    private String username;
    @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST})
    private List<User> associatedSenders;
    @ManyToMany(mappedBy = "associatedSenders")
    private List<User> associatedReceivers;

    //
    // Associated Senders
    //
    public List<User> getAssociatedSenders() {
        if (associatedSenders == null) {
            associatedSenders = new ArrayList<User>();
        }

        return associatedSenders;
    }

    public void addAssociatedSender(User sender) {
        if (associatedSenders == null) {
            associatedSenders = new ArrayList<User>();
        }

        associatedSenders.add(checkNotNull(sender));
        if (!sender.getAssociatedReceivers().contains(this)) {
            sender.addAssociatedReceiver(this);
        }
    }

    public void removeAssociatedSender(User sender) {
        if (associatedSenders == null) {
            associatedSenders = new ArrayList<User>();
        }

        associatedSenders.remove(checkNotNull(sender));
        if (sender.getAssociatedReceivers().contains(this)) {
            sender.removeAssociatedReceiver(this);
        }
    }

    public void setAssociatedSenders(List<User> senders) {
        checkNotNull(senders);

        if (associatedSenders == null) {
            associatedSenders = new ArrayList<User>();
        }

        // first remove all previous senders
        for (Iterator<User> it = associatedSenders.iterator(); it.hasNext();) {
            User sender = it.next();
            it.remove();
            if (sender.getAssociatedReceivers().contains(this)) {
                sender.removeAssociatedReceiver(this);
            }
        }

        // now add new senders
        for (User sender : senders) {
            addAssociatedSender(sender);
        }
    }

    //
    // Associated Receivers
    //
    public List<User> getAssociatedReceivers() {
        if (associatedReceivers == null) {
            associatedReceivers = new ArrayList<User>();
        }

        return associatedReceivers;
    }

    /**
     * <p><b>Note:</b> this method should not be used by clients, because it
     * does not manage the inverse side of the JPA relationship. Instead, use
     * the appropriate method at the inverse of the relationship.
     * 
     * @param receiver
     */
    protected void addAssociatedReceiver(User receiver) {
        if (associatedReceivers == null) {
            associatedReceivers = new ArrayList<User>();
        }

        associatedReceivers.add(checkNotNull(receiver));
    }

    /**
     * <p><b>Note:</b> this method should not be used by clients, because it
     * does not manage the inverse side of the JPA relationship. Instead, use
     * the appropriate method at the inverse of the relationship.
     *
     * @param receiver
     */
    protected void removeAssociatedReceiver(User receiver) {
        if (associatedReceivers == null) {
            associatedReceivers = new ArrayList<User>();
        }

        associatedReceivers.remove(checkNotNull(receiver));
    }
}

当我向associatedSenders集合中添加新用户实体时,一切都按预期工作。数据库中的表正确更新,内存中的关系也正确。但是,当我从associatedSenders集合(或该集合中的所有实体)中删除用户实体时,例如做这样的电话:

List<User> senders = Collections.emptyList();
user.setAssociatedSenders(senders)

数据库表格正确更新,但下次调用em.find(User.class, username),其中username是先前位于associatedSenders集合中的用户的ID,显示associatedReceivers 1}}集合(反面)尚未正确更新。也就是说,user仍在该集合中。仅当我通过em.refresh()刷新实体时,才会正确更新集合。看起来实体管理器在这里进行了一些缓存,但这种行为对我来说似乎不正确。

UPDATE 可能值得一提的是,我正在修改JSF托管bean中前端的用户实体,即实体处于分离状态时。

2 个答案:

答案 0 :(得分:3)

如果要在分离对象时修改对象,则必须将其合并()回到持久性单元中。由于要修改源对象和目标对象,因此必须合并()两个对象以维护关系的两侧。由于已删除了对象,因此级联合并是不够的,因此无需级联到。

您还可以在合并之后以及提交之前和之后检查对象的状态。

可能包含您的合并代码。

答案 1 :(得分:0)

我能弄清楚的唯一解释是,将空列表设置为其associatedSender字段的用户对象不是原始缓存对象,它只是一个副本...