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

查看:98
本文介绍了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:

由以下原因引起:org.hibernate.PersistentObjectException:传递给持久对象的分离实体: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个链接中的文章,您需要在双向关系的两侧都修复您的设置器.一侧的设置器示例在

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.

更正设置程序后,您想将实体访问类型声明为属性".声明属性"访问类型的最佳实践是将所有注释从成员属性移至相应的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天全站免登陆