分离的实体未正确合并?

时间:2019-03-08 18:10:36

标签: java hibernate jpa

我需要一些帮助,下面是我的代码。

@Override
public SEDocumentListWidget clone() throws CloneNotSupportedException {
    final SEDocumentListWidget clone = (SEDocumentListWidget) super.clone();
    final Set<SEDocumentListCategoryList> listCopy = new HashSet<>(clone.getDocumentListCategoryList());

    SEEntityManager.flush();
    SEEntityManager.detach(listCopy);

    for (SEDocumentListCategoryList listItem: listCopy) {
        listItem.setOid(UUID.randomUUID().toString());
    }

    final Set<SEDocumentListCategoryList> listCopyMerged = SEEntityManager.getEntityManager().merge(listCopy);
    clone.setDocumentListCategoryList(listCopyMerged);
    return clone;
}

当我运行它时,它引发以下错误:

  

由于:org.hibernate.PersistentObjectException:分离的实体   传递以保留:com.softexpert.dashboard.entity.SEDashboard

这可能很简单,任何帮助将不胜感激,它也似乎是此行的特定问题:

    final Set<SEDocumentListCategoryList> listCopyMerged = SEEntityManager.getEntityManager().merge(listCopy);

@EDIT添加了SEDocumentListCategoryList实体

package com.softexpert.dashboard.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import com.softexpert.platform.annotation.Audit;
import com.softexpert.platform.artefacts.EntityObject;

/**
 * 
 * @author elia.melfior
 *
 */
@Entity
@Audit(dataChange = true, dataLoad = false)
@Table(name = "SEDOCUMENTLISTCATEGORYLIST")

public class SEDocumentListCategoryList extends EntityObject {

    private static final long serialVersionUID = 1L;
    private Integer cdCategory;

    @Column(name = "CDCATEGORY")
    public Integer getCdCategory() {
        return this.cdCategory;
    }

    public void setCdCategory(Integer cdCategory) {
        this.cdCategory = cdCategory;
    }
}

1 个答案:

答案 0 :(得分:2)

查看您的代码,我认为您想复制持久对象并使用新ID持久化它们。在这种情况下,我认为您必须使用persist()而不是merge()(它会尝试更新您的分离实体)。