Search code examples
springspring-data-jparepositoryentitymanager

JPA save/update only if modifications


I have a problem, with the Spring JPA, I did not find anything about saving entity only if there are modification. And I am not talking about saving certain fields.

After the database data is loaded by the JPA entity manager, how can I verify, before I save the entity, if there are modification/changes to the fields. Without taking each field and verify it with: if (field != ...)

    Class i = repo.findByExternalId(externalId);

    ...

    if (modifications)
       repo.save(i);
     else
       //don't save

I tried with dynamic-update=true but it doesn't work.

If you have some information about what to search on the web... that would be helpful as well.

Thanks.


Solution

  • Just don't call save() but put everything in one transaction.

    JPA will do the checking for you and save changes if necessary.