Update entity in jpa

Руслан Иванов :

How can I update every fields in Entity without write sql query:

(update Entity u set u.m1= ?1, u.m2= ?2, ... where u.id = ?3)

Class has 20+ fields and if I write sql query This will take a long time. And I often add new fields

Can I update everything automatically? Like this:

entityRepo.update(entity);

If i do entityRepo.save(); create unnecessary record in base.

davidxxx :

No, you can use JpaRepository.save(S entity) that saves or updates the entity if existing.
To achieve that, make sure that the entity has its JPA @Id valued before invoking save() otherwise a new record will indeed be created.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=142551&siteId=1