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

前端 未结 18 2378
醉酒成梦
醉酒成梦 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:50

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

    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.

    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.

提交回复
热议问题