PersistentObjectException: detached entity passed to persist thrown by JPA and Hibernate

前端 未结 18 2357
醉酒成梦
醉酒成梦 2020-11-22 05:10

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

18条回答
  •  [愿得一人]
    2020-11-22 05:27

    Remove cascading from the child entity Transaction, it should be just:

    @Entity class Transaction {
        @ManyToOne // no cascading here!
        private Account account;
    }
    

    (FetchType.EAGER can be removed as well as it's the default for @ManyToOne)

    That's all!

    Why? By saying "cascade ALL" on the child entity Transaction you require that every DB operation gets propagated to the parent entity Account. If you then do persist(transaction), persist(account) will be invoked as well.

    But only transient (new) entities may be passed to persist (Transaction in this case). The detached (or other non-transient state) ones may not (Account in this case, as it's already in DB).

    Therefore you get the exception "detached entity passed to persist". The Account entity is meant! Not the Transaction you call persist on.


    You generally don't want to propagate from child to parent. Unfortunately there are many code examples in books (even in good ones) and through the net, which do exactly that. I don't know, why... Perhaps sometimes simply copied over and over without much thinking...

    Guess what happens if you call remove(transaction) still having "cascade ALL" in that @ManyToOne? The account (btw, with all other transactions!) will be deleted from the DB as well. But that wasn't your intention, was it?

提交回复
热议问题