I believe the entity that I wish to delete, is a managed entity. But, regardless, why is merging it then removing it giving me the following error:
deleted instance passed to merge
Someone said on stackoverflow that merge should be ignored if it is a managed entity. So why is this not being ignored?
The way I wish to delete it is like so:
TrialUser mergedEntity = em.merge(tu);
em.remove(mergedEntity);
But this errors, but if I get rid of the first line it seems to work fine. But I want it the other way because that is consistent with the rest of the code.
EDIT:
@PersistenceContext(unitName = "UnitName")
protected EntityManager entityManager;
@Table(name="TRIAL_USER")
@Id
private BigDecimal id;
@ManyToOne(cascade= {CascadeType.ALL }, fetch=FetchType.EAGER)
@JoinColumn(name="TRIAL_USER_CLASS_ID3")
private TrialUserElement trialUserElement3;
@ManyToOne(cascade= {CascadeType.ALL }, fetch=FetchType.EAGER)
@JoinColumn(name="TRIAL_USER_CLASS_ID1")
private TrialUserElement trialUserElement1;
@ManyToOne(cascade= {CascadeType.ALL }, fetch=FetchType.EAGER)
@JoinColumn(name="TRIAL_USER_CLASS_ID2")
private TrialUserElement trialUserElement2;
boolean isManaged = entityManager.contains(mergedEntity);
if placed in the middle of merge and remove lines? btw. for the sake of clarity it would be good to paste a proper and compilable code (you have doubledtrialUserElement1
) and split the code snippet into two classes (managing bean annotated with@PersistenceContext
and entity bean annotated with@Entity
).