Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

1. Overview

In this quick tutorial, we're going to take a look at the support for Vavr in Spring Data – which was added in the 2.0.0 Spring build snapshot.

More specifically, we're going to show an example of using Vavr Option and Vavr collections as return types of a Spring Data JPA repository.

2. Maven Dependencies

First, let's setup a Spring Boot project, since it makes configuring Spring Data much quicker, by adding the spring-boot-parent dependency to the pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.2</version>
    <relativePath />
</parent>

Evidently, we also need the vavr dependency, as well as a few other dependencies for Spring Data and testing:

<dependency>
    <groupId>io.vavr</groupId>
    <artifactId>vavr</artifactId>
    <version>0.9.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

The latest versions of vavr, spring-boot-starter-data-jpa, spring-boot-starter-test and h2 can be downloaded from Maven Central.

In this example, we're only using Spring Boot because it provides Spring Data auto-configuration. If you are working in a non-Boot project, you can add the spring-data-commons dependency with Vavr support directly:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-commons</artifactId>
    <version>2.0.0.RELEASE</version>
</dependency>

3. Spring Data JPA Repository With Vavr

Spring Data now contains support for defining repository query methods using Vavr‘s Option and Vavr collections: Seq, Set, and Map as return types.

First, let's create a simple entity class to manipulate:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;
    
    // standard constructor, getters, setters
}

Next, let's create the JPA repository by implementing the Repository interface and defining two query methods:

public interface VavrUserRepository extends Repository<User, Long> {

    Option<User> findById(long id);

    Seq<User> findByName(String name);

    User save(User user);
}

Here, we have made use of Vavr Option for a method returning zero or one results, and Vavr Seq for a query method which returns multiple User records.

We also need a main Spring Boot class to auto-configure Spring Data and bootstrap our application:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Since we have added the h2 dependency, Spring Boot will auto-configure a DataSource using an in-memory H2 database.

4. Testing the JPA Repository

Let's add a JUnit test to verify our repository methods:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class VavrRepositoryIntegrationTest {

    @Autowired
    private VavrUserRepository userRepository;

    @Before
    public void setup() {
        User user1 = new User();
        user1.setName("John");
        User user2 = new User();
        user2.setName("John");

        userRepository.save(user1);
        userRepository.save(user2);
    }

    @Test
    public void whenAddUsers_thenGetUsers() {
        Option<User> user = userRepository.findById(1L);
        assertFalse(user.isEmpty());
        assertTrue(user.get().getName().equals("John"));

        Seq<User> users = userRepository.findByName("John");
        assertEquals(2, users.size());
    }
}

In the above test, we first add two user records to the database, then call the repository's query methods. As you can see, the methods return the correct Vavr objects.

5. Conclusion

In this quick example, we have shown how we can define a Spring Data repository using Vavr types.

As always, the full source code can be found over on GitHub.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Persistence footer banner
Comments are closed on this article!