When use flush with JpaRepository

Viewed 35

I have a REST API project with Spring (5.7.2) Web / Security.

I have just one table discs, it contain a id, and a name.

public class DiscsRepository extends JpaRepository<Discs, Integer> {
}

When I delete a disc, my DiscsService do

discsRepository.deleteById(5);

When I want insert or update I do :

discsRepository.save(myDisc);

I read that flush() is used when you want use the value in same transaction (before commit) and that flush() will synchronize and empty the SQL cache.

In my case I see that I have the value inserted / updated or deleted in my database.

Should I use flush() (I think no) but maybe I mistaken ?

1 Answers

The Insert, Update and Delete SQL statements are usually executed at commit.

If you want to force that you can call flush().

This is for example useful if you have triggers in the database that should be executed just after you call save or delete and not wait until the commit.

Related