[英]Hibernate - “detached entity passed to persist” error for already persisted child
I have an entity, which is already persisted and want to add it to a newly generated parent entity (not yet persisted). 我有一个实体,该实体已经存在,并且想要将其添加到新生成的父实体(尚未保留)中。 If i try to persisted the parent then, i get the error "detached entity passed to persist: model.Child".
如果我尝试保留父对象,则会收到错误“将分离的实体传递给持久对象:model.Child”。 I think i have to somehow call a "entityManager.merge()" for the child instead of a "entityManager.persist()".
我认为我必须以某种方式为孩子调用“ entityManager.merge()”,而不是“ entityManager.persist()”。 But i do not explicitly call the persist.
但是我没有明确地称呼为坚持。 This is handled by the "cascade = CascadeType.ALL" annotation.
这由“ cascade = CascadeType.ALL”注释处理。 Can i tell hibernate somehow to do a merge here, if the entity already exists?
如果该实体已经存在,我可以告诉休眠方式在这里进行合并吗?
By the way: If i first persist the parent, then add the child and then persist the parent again -> It works (But makes my application logic much more complicated). 顺便说一句:如果我先保留父代,然后添加子代,然后再次保留父代->它可以工作(但会使我的应用程序逻辑复杂得多)。
Here my code: 这是我的代码:
public class App
{
@Test
public void test()
{
// I have a child object (in the real app
//this is a user object and already persisted
Child child = new Child();
HibernateHelper.persist(child);
Parent parent = new Parent();
parent.addChildren(child);
// throws the exception "detached entity passed to persist: model.Child"
HibernateHelper.persist(parent);
Parent newParent = HibernateHelper.find(Parent.class, parent.getId());
assertEquals(1, newParent.getChildren().size());
}
}
My "child" entity: 我的“孩子”实体:
@Entity
@Table(name = "child")
public class Child {
public Child(){}
private Long id;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private Parent parent;
@ManyToOne
@JoinColumn
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
My "parent" entity: 我的“父”实体:
@Entity
@Table(name="parent")
public class Parent {
public Parent(){}
private Long id;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private Set<Child> children = new HashSet<Child>();
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL)
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
public void addChildren(Child child){
children.add(child);
child.setParent(this);
}
}
The persist helper method (looks the same for the child) 持久帮助器方法(对于孩子来说看起来一样)
public static void persist(Parent entity){
EntityManager entityManager = null;
try {
entityManager = beginTransaction();
if(entity.getId()!=null){
entityManager.merge(entity);
}else{
entityManager.persist(entity);
}
entityManager.getTransaction().commit();
} catch (Exception e) {
System.out.println(e);
return;
}finally{
if(entityManager != null)
entityManager.close();
}
}
An option would be to always use EntityManager.merge. 一种选择是始终使用EntityManager.merge。 In case a new entity is passed it is persisted, in case a detached entity is passed it is merged into the current persistence context.
如果传递了新实体,则将其持久化;如果传递了分离实体,则将其合并到当前持久性上下文中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.