Home > OS >  Spring data update just one entity field
Spring data update just one entity field

Time:08-24

i’m new to spring when i try to update just one field of an entity I noticed in logs that hibernate perform two queries, before update it does a SELECT of all fields. Is that ok? Why does Hibernate perform that SELECT? How can i update a field with just one UPDATE query? Additionally when I tried to update a single title in an entity that has another nested entity i end up with a bunch of SELECT. I think it’s not good for performance or I’m wrong?

Something s = somethingRepository.findById(id);
s.setField1(someData);
somethingRepository.save(s);

On the internet I found a solution to make custom query with @Modifying and @Query(“UPDATE …”) but in this way I need to make custom query for every single field. Is there a better solution?

CodePudding user response:

As per the source code you have pasted in the question

Something s = somethingRepository.findById(id);
s.setField1(someData);
somethingRepository.save(s);

if the entity Something which you are asking does not exist in the hibernate first level cache, it will make one SELECT call.

and as you are updating field 1 value then it will make another update call. It does not matter if you are using save or not because Hibernate dirty checks will ensure that all changes are updated.

otherwise you can use custom method with @Modifying with JPQL and named params. It is more readable than ?1,

@Modifying
@Query("UPDATE Something s SET s.field = :fieldValue WHERE s.id = :id")
void updateField(String fieldValue, UUID id);

Regarding that you are seeing multiple calls "when I tried to update a single title in an entity that has another nested entity". It depends on how you have created the relationship among entities. If you can share the entities and their relationship then only it can be answered accurately.

CodePudding user response:

Because internally the repository.save() method does an upsert. If you go to the inner classes and check you will see that first, it will check whether the entity is present in the database then based on that it will perform add or update. If you don't want the SELECT query to run then you can use the native query offered by the JPA repository. You can do something like this:

@Modifying
@Transactional
@Query("UPDATE <tableName> SET <columnName> = ?1 WHERE <condition>" ,nativeQuery=true)
void updateSomething(String value);
  • Related