Hibernate updating my entities

前端 未结 1 1224
一整个雨季
一整个雨季 2021-01-28 04:04

I have created 2 entities. Now when I retrieve my entities in my backend I verify some things, but when my backend return those entities to my frontend the hibernate try to upd

相关标签:
1条回答
  • 2021-01-28 04:46

    You already got the pieces in the comments, but let me put it together in an answer:

    I think it`s because I change some data in my entity while i do my logic

    That interpretation is correct.

    If you load an entity via JPA it is attached to a session and every change to it gets tracked and written to the database once you close the session/ commit the transaction.

    In order to avoid that you need to remove the entity from the session. You can do that by either

    • detaching the entity from the persistence context: https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#detach-java.lang.Object-

    • closing the EntityManager / transaction before doing the manipulation. This basically means doing your changes to the entity outside the outer most method annotated with @Transactional

    Note that in both cases lazy loading won't work anymore so you'll have to make sure you loaded everything you needed before detaching the entity.

    0 讨论(0)
提交回复
热议问题