Eliâ Melfior
Eliâ Melfior

Reputation: 369

Detached entity is not being merged correctly?

i need some help, my code is below.

@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;
}

When i run it, it throws the following error:

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.softexpert.dashboard.entity.SEDashboard

It might be something very simple, any help would be appreciated, it also looks like a specific problem with this line:

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

@EDIT Added the SEDocumentListCategoryList entity

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;
    }
}

Upvotes: 0

Views: 48

Answers (1)

sgraton
sgraton

Reputation: 106

Looking at your code I think you want to copy persistent objects and persist them with a new id. In this case, i think you must use persist() instead of merge() (which tries to update your detached entities).

Upvotes: 2

Related Questions