PersistentObjectException:传递给 JPA 和 Hibernate 抛出的持久化的分离实体 [英] PersistentObjectException: detached entity passed to persist thrown by JPA and Hibernate

查看:38
本文介绍了PersistentObjectException:传递给 JPA 和 Hibernate 抛出的持久化的分离实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多对一关系的 JPA 持久化对象模型:一个 Account 有许多 Transactions.一个 Transaction 有一个 Account.

I have a JPA-persisted object model that contains a many-to-one relationship: an Account has many Transactions. A Transaction has one Account.

以下是代码片段:

@Entity
public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(cascade = {CascadeType.ALL},fetch= FetchType.EAGER)
    private Account fromAccount;
....

@Entity
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToMany(cascade = {CascadeType.ALL},fetch= FetchType.EAGER, mappedBy = "fromAccount")
    private Set<Transaction> transactions;

我能够创建一个 Account 对象,向其中添加交易,并正确地保存 Account 对象.但是,当我创建一个交易,使用一个现有的已经持久化的帐户,并持久化交易,我得到一个例外:

I am able to create an Account object, add transactions to it, and persist the Account object correctly. But, when I create a transaction, using an existing already persisted Account, and persisting the the Transaction, I get an exception:

Caused by: org.hibernate.PersistentObjectException: detached entity传递给persist: com.paulsanwald.Account在 org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.paulsanwald.Account at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)

因此,我可以保留包含交易的 Account,但不能保留具有 Account 的交易.我以为这是因为 Account 可能没有附加,但这段代码仍然给我同样的异常:

So, I am able to persist an Account that contains transactions, but not a Transaction that has an Account. I thought this was because the Account might not be attached, but this code still gives me the same exception:

if (account.getId()!=null) {
    account = entityManager.merge(account);
}
Transaction transaction = new Transaction(account,"other stuff");
 // the below fails with a "detached entity" message. why?
entityManager.persist(transaction);

如何正确保存与已经持久化的 Account 对象关联的 Transaction?

How can I correctly save a Transaction, associated with an already persisted Account object?

推荐答案

这是一个典型的双向一致性问题.此链接以及此链接.

This is a typical bidirectional consistency problem. It is well discussed in this link as well as this link.

根据前 2 个链接中的文章,您需要在双向关系的两侧修复您的 setter.一侧的示例设置器位于 此链接.

As per the articles in the previous 2 links you need to fix your setters in both sides of the bidirectional relationship. An example setter for the One side is in this link.

多方面的示例设置器位于 此链接.

An example setter for the Many side is in this link.

在您更正 setter 之后,您希望将实体访问类型声明为属性".声明属性"访问类型的最佳实践是将所有注释从成员属性移动到相应的 getter.一个重要的警告是不要在实体类中混合字段"和属性"访问类型,否则 JSR-317 规范未定义行为.

After you correct your setters you want to declare the Entity access type to be "Property". Best practice to declare "Property" access type is to move ALL the annotations from the member properties to the corresponding getters. A big word of caution is not to mix "Field" and "Property" access types within the entity class otherwise the behavior is undefined by the JSR-317 specifications.

这篇关于PersistentObjectException:传递给 JPA 和 Hibernate 抛出的持久化的分离实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆