问题描述
我现在有这个问题已经很久了,我已经搜索了网页,并且进出了网页,但是还没有找到解决方案。我希望你能帮助我。
我有两个实体之间的父子关系,如下所示:
@Entity
public class Parent {
// ...
@OneToMany(mappedBy =parent,fetch = FetchType.LAZY,cascade = CascadeType.REMOVE)
private Set< Child> children = new HashSet< Child>();
// ...
}
@实体
公共类子元素{
// ...
@ManyToOne(fetch = FetchType.LAZY)
私有父母;
// ...
}
当我创建一个新的孩子并将其分配给父母时,父母在缓存中时不会进行更新。
父亲=新父();
em.persist(parent);
// ...
Child child = new Child();
child.setParent(parent);
em.persist(child);
parent.getChildren()。size(); //返回0
我曾尝试使用@PreUpdate自动将子项添加到父项孩子是持久的,但是当我们有两个不同线程的实体管理器(如JBoss)时,问题依然存在,直到我们调用 em.refresh(parent)
所以问题是 - 是否有一种方法可以顺利消除问题并确保 parent.getChildren()
总是返回最新的孩子列表?
大多数ORM的行为都是这样的。 b
$ b
缓存中的对象不会从数据库更新(不需要额外读取)。也可以将对象模型和持久性看作是分开的。即保持你的对象模型与自身一致,不要依赖持久化机制来为你做这件事。
所以如果你想把对象添加到集合中然后在setParent代码中执行。
这种情况下的最佳做法实际上是让关系的一方完成所有工作,让另一方推迟到它。此外,我建议使用字段访问而不是方法访问,这样您可以更灵活地自定义方法。
向父级添加名为addChild的方法
public void addChild(Child child){
child.setParent0(this);
getChildren()。add(individualNeed);
}
然后在Child中设置setParent:
public void setParent(Parent parent){
parent.addChild(child);
}
Child中的setParent0是父级子级的属性stter。
public void setParent0(父母){
this.parent = parent;
}
我还建议getChildren方法返回一个不可变的集合,开发人员不会无意中不使用这种方法(我在所有这些方面学到了很难的方法)。
还有一件事,你应该使用null检查代码和其他防御性部分在上面的代码中,为了清晰起见,我将其省略了。
I have this problem for a long time now, I have searched the web and SO in and out and didn't find a solution yet. I hope you can help me on that.
I have a parent-child relationship between two entities like the following:
@Entity
public class Parent {
// ...
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
private Set<Child> children = new HashSet<Child>();
// ...
}
@Entity
public class Child {
// ...
@ManyToOne(fetch = FetchType.LAZY)
private Parent parent;
// ...
}
The thing is that when I create a new child and assign it to a parent, the parent doesn't get updated when it is in the cache already.
Parent parent = new Parent();
em.persist(parent);
// ...
Child child = new Child();
child.setParent(parent);
em.persist(child);
parent.getChildren().size(); // returns 0
I have tried to use @PreUpdate to automatically add the child to the parent when the child is persisted, but in the case when we have 2 entity managers in 2 different threads (like in JBoss), the issue still exists, until we call em.refresh(parent)
So the question is - is there a way to smoothly eliminate the problem and ensure that parent.getChildren()
always return the up-to-date list of children?
Most ORM's will behave this way.
The object in the cache is not updated from the database (an extra read that is not necessary). Also think of the object model and the persistence as separate. i.e. keep your object model consistent with itself and don't rely on the persistence mechanism to do this for you.
So if you want the object to be added to the collection then do that in the "setParent" code.
The best practice in this case is in fact to make one side of the relationship do all the work and let the other side defer onto it. Also I would suggest using field access rather than method access, that way you can customise methods with greater flexibility.
Add a method to parent called addChild
public void addChild(Child child) {
child.setParent0(this);
getChildren().add(individualNeed);
}
and then make setParent in Child:
public void setParent(Parent parent) {
parent.addChild(child);
}
setParent0 in Child is the property stter for parent on child.
public void setParent0(Parent parent) {
this.parent = parent;
}
I would also suggest that the "getChildren" method return an immutable collection so that developers don't inadvertantly not use this method (I learnt the hard way in all of this).
One more thing, you should have null checking code and other defensive pieces in the above code, I left it out for clarity.
这篇关于Hibernate @OneToMany与mappedBy(父 - 子)关系和缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!