Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
like image 727
Rika Avatar asked Oct 15 '14 17:10

Rika


People also ask

What does em merge do?

The EntityManager. merge() operation is used to merge the changes made to a detached object into the persistence context. merge does not directly update the object into the database, it merges the changes into the persistence context (transaction).

What is merge in JPA?

JPA's merge method copies the state of a detached entity to a managed instance of the same entity. Hibernate, therefore, executes an SQL SELECT statement to retrieve a managed entity from the database.

What is em persist?

em.persist(s1); persist() - This method is used to make an instance managed and persistent. An entity instance is passed within this method.


2 Answers

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.

like image 130
Spyna Avatar answered Oct 19 '22 18:10

Spyna


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.

like image 5
Dick Chesterwood Avatar answered Oct 19 '22 17:10

Dick Chesterwood