Java 为什么我在首先合并实体时将删除的实例传递给合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26388848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:24:41  来源:igfitidea点击:

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

javajpamergeentitymanager

提问by Rika

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?

有人在 stackoverflow 上说如果是托管实体,应该忽略合并。那么为什么这不被忽视呢?

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;

回答by Dick Chesterwood

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.

在事务提交的时候,remove 被级联到链接的实体。

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.

尝试更改您的级联规则 - 将其拉回 CascadeType.MERGE(对于所有三个),看看您是否仍然遇到异常。或者更改为 CascadeType.DELETE,这将阻止必要的合并被级联。

回答by uaiHebert

The JPA SPEC says that:

JPA SPEC 说:

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).

3.2.7.1 合并分离的实体状态

如果 X 是已删除的实体实例,则合并操作将抛出 IllegalArgumentException(否则事务提交将失败)。

That is why you cannot merge a deleted entity.

这就是您不能合并已删除实体的原因。

回答by Spyna

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

当您在事务中运行一些代码时,当您在最后提交该方法时,您可能会遇到此错误。在带有注释的方法或类中使用 spring 时

@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.

为避免该错误,您不应删除并保存在同一事务中。