![](/img/trans.png)
[英]Hibernate: org.hibernate.PersistentObjectException: detached entity passed to persist
[英]Entity manager batch update gives org.hibernate.PersistentObjectException: detached entity passed to persist
我嘗試關注這篇文章,因為通過批量更新,我使用合並獲得了不必要的選擇:
for(int i = 0; i< elements.size(); i++){
Query q = em.createQuery("select p from Parent p join fetch p.child c where p.id=:id", Parent.class);
//parameter of :id from elements
Parent p = q.getSingleResult();
p.setName("aname"); //for simplicity just the name member
myList.add(p);
}
utx = com.arjuna.ats.jta.UserTransaction.userTransaction();
utx.begin();
em.joinTransaction();
for(int i = 0; i< myList.size(); i++){
if (i > 0 && i % Common.BATCH_SIZE == 0) { //BATCH_SIZE=50
em.flush();
em.clear();
}
Parent myObject = myList.get(i);
em.merge(myObject);
}
utx.commit();
這是我的實體定義:
@Entity
@Table(name = "myparenttable", schema = "myschema", catalog = "mydb")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Parent implements Serializable {
private Integer id_parent;
private String name;
@JsonManagedReference
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private List<Child> children;
//getters and setters
@JsonInclude(JsonInclude.Include.NON_NULL)
@OneToMany(mappedBy = "parent", targetEntity = Child.class, fetch = FetchType.LAZY,cascade = {CascadeType.PERSIST, CascadeType.MERGE})
public Set<Child> getChildren() {
return this.children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
}
@Entity
@Table(name = "mychildtable", schema = "myschema", catalog = "mydb")
public class Child implements Serializable {
private Integer id_child;
private String description;
@JsonBackReference
private Parent parent;
//getters and setters
}
在提到的文章之后,我像這樣更改了我的代碼:
utx = com.arjuna.ats.jta.UserTransaction.userTransaction();
utx.begin();
em.joinTransaction();
Session session = em.unwrap( Session.class );
for(int i = 0; i< myList.size(); i++){
if (i > 0 && i % Common.BATCH_SIZE == 0) { //BATCH_SIZE=50
em.flush();
em.clear();
}
Parent myObject = elements.get(i);
session.update(myObject);
}
utx.commit();
當列表小於 BATCH_SIZE (<50) 時,編輯正確,但是,flush 和 clear 語句是必要的,我收到此錯誤:
detached entity passed to persist: Child
我也試過:
session.flush();
session.clear();
有完全相同的錯誤。 有什么我想念的嗎?
您是否檢查過列表是否包含重復的對象? 如果一批嘗試更新具有相同標識符的對象兩次,您可能會遇到此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.