gpt4 book ai didi

java - JPA : removing from a collection中管理双向关系的问题

转载 作者:行者123 更新时间:2023-12-04 06:37:49 25 4
gpt4 key购买 nike

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

@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集合(反面)未正确更新。即, user仍然在那个集合中。仅当我通过 em.refresh() 刷新实体时,该集合已正确更新。看起来实体管理器在这里做了一些缓存,但这种行为对我来说似乎不正确。

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

最佳答案

如果您在分离时修改对象,则必须将其合并()回持久性单元。由于您正在修改源对象和目标对象,因此您必须 merge() 两个对象以维护双方的关系。级联合并是不够的,因为您已经删除了对象,因此没有什么可以级联的。

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

也许包括您的合并代码。

关于java - JPA : removing from a collection中管理双向关系的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4648665/

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