Use Vavr List and Option in Spring 6 JPA repository
In previous articles, we saw how to use Vavr List and Option with Hibernate Now, let’s see how to use Vavr List and Option data types with Spring JPA Repository. The goal is to be able to write this:
Thanks to this Vavr List, you can enjoy concise functional code in your app.
You can for instance use the methods List.map()
, List.flatMap()
or List.filter()
without the whole .stream()
and .collect()
boilerplate code.
To do this, we just need to override some Spring JPA functions. First, add this dependency to your pom.xml
:
A good place to load the custom code is in the Spring config function:
Indeed this code is executed before the persistence layer instantiation.
You can find the whole DataSourceConfig
code in Github.
You can see a full working Spring Boot app that uses these strategies in my Github repo. You may leave a question in the issues if you need.