JPA / Hibernate:传递给persist的分离实体 [英] JPA/Hibernate: detached entity passed to persist

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

问题描述

我有一个包含多对一关系的JPA持久化对象模型:一个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.

以下是一段代码:

Here's a snippet of the code:

@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 passed to persist: com.paulsanwald.Account
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141) 

因此,我可以坚持包含交易的帐户,但不是具有帐户的交易。我认为这是因为帐户可能没有附加,但是这段代码仍然给我带来了同样的异常:

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?

推荐答案

这是一个典型的双向一致性问题。在此链接以及< a href =http://notesonjava.wordpress.com/2008/11/03/managing-the-bidirectional-relationship/ =noreferrer>此链接。

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

根据前两个链接中的文章,您需要在双向关系的两侧修复您的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之后,你想声明Entity访问类型为Property。声明属性访问类型的最佳做法是将所有注释从成员属性移动到相应的获取者。值得警惕的是,不要在实体类中混合使用Field和Property访问类型,否则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.

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

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