list JPA OneToMany - 添加/删除对象后列表不会更新,但会保留在数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14207731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA OneToMany - List doesn't update after add/delete object, but is persisted in DB
提问by user1956764
I'm using JPA not Hibernate. I have a Problem with my Relationship as follows. I have a 1:N Relationship (Parent:Child).
我使用的是 JPA 而不是 Hibernate。我的关系有如下问题。我有一个 1:N 关系(父母:孩子)。
On a JSP-Site, the Parent and the Children are displayed. If I load the JSP-Site (for the first time) and save a Parent with a Child, all is ok. The Parent is saved and the Child is saved too to the DB with the foreign key.
在 JSP 站点上,显示父项和子项。如果我加载 JSP-Site(第一次)并保存一个带有子级的父级,一切正常。父级被保存,子级也用外键保存到数据库中。
After that, a second JSP-Site is displayed with the Parent and Children.
之后,第二个 JSP 站点与父子节点一起显示。
But if I go back to the first Site and save a second Child (adding Child) to existing Parent, the childEntityList
is still the old one, I mean, the new Child is not inside this List, but inserted in the database.
但是如果我回到第一个站点并将第二个子节点(添加子节点)保存到现有的父节点,childEntityList
它仍然是旧的,我的意思是,新的子节点不在这个列表中,而是插入到数据库中。
Why the childEntityList
is old and doesn't update?
为什么childEntityList
旧的不更新?
The Parent:
家长:
@Entity
@Table(schema = "test", name = "Parent")
@XmlRootElement
@NamedQueries({...})
public class Parent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "A_SEQ")
@Basic(optional = false)
@NotNull
@Column(name = "ID")
private int id;
@OneToMany(mappedBy = "p", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<ChildEntity> childEntityList = new ArrayList<ChildEntity>();
// ...
public List<ChildEntity> getChildEntityList() {
return childEntityList;
}
public void setChildEntityList(List<ChildEntity> childEntityList) {
this.childEntityList = childEntityList;
}
public void addChildEntityToParent(ChildEntity c) {
this.childEntityList.add(c);
if(c.getParent() != this) {
c.setParent(this);
}
}
public void removeChildEntityFromParent(ChildEntity c) {
childEntityList.remove(c);
}
// ...
}
The Child:
孩子:
@Entity
@Table( name = "Child" )
@NamedQueries({...})
public class ChildEntity implements Serializable {
@Id
@GeneratedValue( strategy = GenerationType.IDENTITY )
@Column( name = "CHILD_ID" )
private Long childId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID")
private Parent p;
// ...
public Parent getParent() {
return p;
}
public void setParent(Parent p) {
this.p= p;
if(!p.getChildEntityList().contains(this)) {
p.getChildEntityList().add(this);
}
}
Save-Code:
保存代码:
Parent p = new Parent(...);
ChildEntity c = new ChildEntity(...);
p.addChildEntityToParent(c);
childEntityFacade.create(c);
View-Code:
查看代码:
// ...
public doSomething(Parent p) {
super(p);
}
// ...
List<ChildEntity> cList = p.getChildEntityList();
System.out.print("Size: " + c.size());
// ...
In the View-Code, the size of the childEntityList
is every time the same even I add a Child.
Only when I restart the server, all Children of the Parent are displayed.
在视图代码中,childEntityList
即使我添加了一个孩子,每次的大小都是一样的。只有当我重新启动服务器时,才会显示父级的所有子级。
When is the childEntityList
in the Parent Class filled?
Is it automatically, because of the @OneToMany
?
childEntityList
父类中的什么时候被填满?它是自动的,因为@OneToMany
?
Can someone explain it?
有人可以解释一下吗?
Update:Should I merge the EntityManager every time when I add/remove a Child object from the Parent list or have I do some other things?
更新:每次从父列表中添加/删除子对象时,我是否应该合并 EntityManager 或者我是否做了其他一些事情?
回答by James
回答by J Slick
(I'm using JPA not Hibernate!)
(我使用的是 JPA 而不是 Hibernate!)
I guess you meant that you are not using the Hibernate API.
JPA is only a persistence API specification and, by itself, does nothing.
So, if you're using JPA... you're using somethingto do the persistence.
我猜你的意思是你没有使用 Hibernate API。
JPA 只是一个持久性 API 规范,它本身什么都不做。
所以,如果你使用 JPA ......你正在使用一些东西来做持久化。
JPA delegates the persistence work to a persistence engine such as Hibernate, EclipseLink, OpenJPA or something else.
JPA 将持久性工作委托给持久性引擎,例如 Hibernate、EclipseLink、OpenJPA 或其他东西。
You can configure your app to use JPA interfaces with a specific persistence provider, or if the app is running in a JEE container, then the JPA implementation should be provided for you: GlassFish (uses OpenJPA), WebLogic (uses EclipseLink), TomEE (uses OpenJPA).
您可以将您的应用程序配置为使用带有特定持久性提供程序的 JPA 接口,或者如果应用程序在 JEE 容器中运行,则应为您提供 JPA 实现:GlassFish(使用 OpenJPA)、WebLogic(使用 EclipseLink)、TomEE(使用 OpenJPA)。
Should i merge the EntityManager every time when i add/remove a Child Object from the Parent List...
每次从父列表中添加/删除子对象时,我是否应该合并 EntityManager...
You don't merge the EntityManager; you use it to merge the parent entity:
您不合并 EntityManager;您使用它来合并父实体:
parent = em.merge(parent);
Relating to your question, when you use merge() in this way... (and because the mapping specifies CascadeType.ALL) then upon reloading page ("Site 1") you should get the refreshed parent object with an updated object network containing changes you made prior to merge(parent), including any newly added children.
关于您的问题,当您以这种方式使用 merge() 时......(并且因为映射指定了 CascadeType.ALL)然后在重新加载页面(“站点 1”)时,您应该使用更新的对象网络获取刷新的父对象,其中包含您在合并(父)之前所做的更改,包括任何新添加的子项。
回答by vivek kumar
Yes, you need to merge parent entity. I was having same issue and it got resolved once I merged parent entity. Also you need to maintain both sides of the relationship.
是的,您需要合并父实体。我遇到了同样的问题,一旦我合并了父实体,它就得到了解决。您还需要维护双方的关系。