PersistentObjectException:由JPA和Hibernate传递给持久性的分离实体 [英] PersistentObjectException: detached entity passed to persist thrown by JPA and 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.
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屋!