Fabio Ebner
Fabio Ebner

Reputation: 2783

Hibernate updating my entities

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

Answers (1)

Jens Schauder
Jens Schauder

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

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

Related Questions