11

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;
Rika
  • 768
  • 1
  • 5
  • 16
  • Can you post your full JPA session (from the start to the close of the EntityManager, and the salient parts of your Entity class (ie the ID property and any relationships). – Dick Chesterwood Oct 15 '14 at 17:52
  • It has been edited. I use persistence context, so closing and commiting is unnecessary. – Rika Oct 15 '14 at 18:02
  • To make it clear - you are using transaction-scoped container-managed persistence context. What value gives you `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 doubled `trialUserElement1`) and split the code snippet into two classes (managing bean annotated with `@PersistenceContext` and entity bean annotated with `@Entity`). – wypieprz Oct 15 '14 at 19:15

4 Answers4

23

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.

Spyna
  • 490
  • 3
  • 12
5

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.

Dick Chesterwood
  • 2,629
  • 2
  • 25
  • 33
0

I faced the same issue, I discovered that during the update of the parent entity I was using cascade delete annotation at the child entity, so when I tried to delete this child, it triggered the cascade delete for the parent. then got this error. To resolve the issue just remove the cascade delete from the child entity.

Hany Sakr
  • 2,591
  • 28
  • 27
-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.

uaiHebert
  • 1,882
  • 11
  • 21
  • 1
    I think the downvotes [not from me btw] might be because the quoted part of the spec is referencing the opposite scenario to the question. Ie you can't remove and then merge. But you can merge and then remove, which is what the questioner is attempting (in fact in many scenarios this is the only way to delete an object/row). – Dick Chesterwood Nov 17 '17 at 17:49