mailwal

My pet peeve with Spring Data JPA

Thorben

Hey,

You know I love using Spring Data JPA!

But there's one thing we need to talk about ...

the saveAndFlush() method of the JpaRepository.

I see developers misusing it all the time. And I totally understand why.

The word save indicates that you need to call this method to persist any change in the database. But as I hope you know, you only need to call it to persist new entity objects. Changes on managed entity objects get persisted automatically.

And to make it even worse, the AndFlush postfix makes it seem like triggering a flush programmatically is a good or at least common thing. But that's not the case. The automatic flush process has been optimized for more than 20 years and for 99% of the situations, Hibernate handles it much better on its own.

Speak soon,

Thorben

  1. Check out the latest tutorials on the blog.
    Stay up to date with the latest releases and features in the Java persistence world. Read the blog.

  2. Watch my tutorials on YouTube.
    You prefer videos over written content? Then my YouTube channel is the right place for you.

  3. Get my Hibernate Tips ebook.
    The ebook is packed with ready-to-use solutions to help you solve your next Hibernate problem even quicker. Get it here.

  4. Join the Persistence Hub.
    The Persistence Hub teaches you everything you need to know on your journey to becoming a Java persistence expert. Get access to video courses, Expert Sessions with amazing guests, monthly Java Persistence News, and more. Become a member.

Thorben Janssen, Elser Bruch 63, 33106, Paderborn, Germany