SPRING JPA with Hibernate updating all instances of an entity, even if persistence is called with a single object

I am using Spring JPARepository with hibernation and have one question about updating the entity. I call jparepository.save (entity), passing in a single object, but in the trace logs I can see update instructions issued for other rows in the database. Before calling save, I have findAll, and the value of some objects changes. but I only pass one object to save, but all updated objects get saved. Can you provide any information about this.

+1
source share
1 answer

When loading objects from the database, these objects are "managed objects". If you change the "managed objects" (and send the transaction later), you do NOT need to keep them explicit. (This is what β€œmanaged” means).

But Hibernate will not immediately update the database, instead it will wait:

  • you are making a transaction
  • you call EntityManager.flushor
  • You are executing a database query or saving sleep mode! (when you go around Hibernate and execute a query, for example, using a simple JDBC / Spring JDBC template, then you need to call first EntityManager.flushif you do not see non-jet flushed data ( fooobar.com/questions/1584277 / ... ))

And this is what you observed:

  • 1) you load some objects, so they become manageable
  • 2) , hibernate sql
  • 3) - , .
+3

Source: https://habr.com/ru/post/1584278/


All Articles