Reputation: 2783
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 update automatically the db.
I don't actually run any update in my backend code (I think it`s because I change some data in my entity while i do my logic and remove some properties that I should return to my frontend).
How can I tell to hibernate to not update anything until I run .save or .update explicitly?
Upvotes: 0
Views: 2528
Reputation: 81862
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.
Upvotes: 3