Why am I getting deleted instance passed to merge, when merging the entity first

送分小仙女□ 提交于 2019-12-22 05:48:09

问题


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;

回答1:


You can have this error when you run some code into a transaction, when you commit at the end og the method. When using spring in a method or class annotated with

@Transactional

This happens because you first delete the object (without committing) and then try to update it.

this code will generate the exception:

    @Transactional
    myethod(){
      dao.delete(myObject);
      myObject.setProperty("some value");
      dao.save();  
    }

To avoid the error you should not delete and then save in the same transaction.




回答2:


This is bit of a shot in the dark as I can't run your code, and these types of problems can turn out to be a bit complex. But rest assured that it should be fine to merge and then delete. I suspect it may be related to your many to one associated entities.

At the point where the transaction commits, the remove is being cascaded to the linked entities.

Even though the merge is redundant for your parent entity, I think the merge is being cascaded to the child entities, which have been deleted, hence the exception.

Try changing your cascade rules - pull it back to CascadeType.MERGE (for all three) and see if you still get the exception. Or change to CascadeType.DELETE, this will prevent the necessary merge being cascaded.




回答3:


The JPA SPEC says that:

3.2.7.1 Merging Detached Entity State

If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).

That is why you cannot merge a deleted entity.



来源:https://stackoverflow.com/questions/26388848/why-am-i-getting-deleted-instance-passed-to-merge-when-merging-the-entity-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!