How To Map String To Boolean in Spring Data JPA

Overview

There are times you work with legacy databases that store boolean values as strings. Let’s consider this example: Town A conducted a survey to see if their citizens like apples or not. The developer back then created a table like this:

SQL

For the file like_apple, he planned to store LIKE and DONOT as values (who knows why he did that). The task now is to map the such string to boolean values so other developers can create API interacting with that legacy data.

It turned out, JPA has quite an elegant support for this case. Let’s find out how to convert string to boolean and vice versa.

Using @Converter

To convert between two data types in JPA, you need to create a converter. In this case, this is the implementation:

Java

To define a JPA converter, you need to:

  • Create a class and annotate it will @Converter
  • Make that class implement the interface AttributeConverter.

You may notice autoApply is set to false. This prevents the converter from applying to all fields (since this situation involves only one field).

Next, in the entity class, specify that the boolean field needs this converter to work properly:

Java

You specify the converter by annotating the field you want to convert with @Convert and specify the converter class.

This is all you need to do. Next, let’s see how the conversion work.

Test the converter

Let’s create a controller to accept POST and GET requests to demonstrate setting and getting data to and from the database.

Java

As you can see, I created two endpoints to create and list the records here. First, let’s create some records using Postman:

Create a new record using standard REST endpoint
Create a new record using standard REST endpoint

Now, let’s try the list endpoint:

Get all entries from the database
Get all entries from the database

The API works as expected. Let’s check the data in the database:

Data stored as string in the database
Data stored as string in the database

As you can see, in the database, data for the field like_apple is still stored as a string.

Conclusion

As you can see, by using the @Converter, @Convert, and the AttributeConverter interface, it was quite easy to convert String to Boolean and back with Spring Data JPA.

As usual, the code for this article is available on Github.

Leave a Comment