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

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

    My Spring Data JPA-based answer: I simply added a @Transactional annotation to my outer method.

    Why it works

    The child entity was immediately becoming detached because there was no active Hibernate Session context. Providing a Spring (Data JPA) transaction ensures a Hibernate Session is present.

    Reference:

    https://vladmihalcea.com/a-beginners-guide-to-jpa-hibernate-entity-state-transitions/

提交回复
热议问题