Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

Spring Data JPA queries, by default, are case-sensitive. In other words, the field value comparisons are case-sensitive.

In this tutorial, we’ll explore how to quickly create a case insensitive query in a Spring Data JPA repository.

2. Dependencies

Firstly, let’s make sure we have Spring Data and H2 database dependencies in our pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>3.3.2</version>
</dependency>
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    <version>2.1.214</version>
</dependency>

The latest versions of these Spring Boot Starter JPA and H2 are available on Maven Central.

3. Initial Setup

Let’s say we have a Passenger entity with id, firstName, and lastName attributes:

@Entity
class Passenger {
 
    @Id
    @GeneratedValue
    @Column(nullable = false)
    private Long id;
 
    @Basic(optional = false)
    @Column(nullable = false)
    private String firstName;
 
    @Basic(optional = false)
    @Column(nullable = false)
    private String lastName;
 
    // constructor, static factory, getters, setters
}

Also, let’s prepare our test class by populating the database with some sample Passenger data:

@DataJpaTest
@RunWith(SpringRunner.class)
public class PassengerRepositoryIntegrationTest {

    @PersistenceContext
    private EntityManager entityManager;
    @Autowired
    private PassengerRepository repository;

    @Before
    public void before() {
        entityManager.persist(Passenger.from("Jill", "Smith"));
        entityManager.persist(Passenger.from("Eve", "Jackson"));
        entityManager.persist(Passenger.from("Fred", "Bloggs"));
        entityManager.persist(Passenger.from("Ricki", "Bobbie"));
        entityManager.persist(Passenger.from("Siya", "Kolisi"));
    }
    
    //...
}

4. IgnoreCase for Case Insensitive Queries

Now, suppose we want to perform a case-insensitive search to find all passengers with a given firstName.

To do so, we’ll define our PassengerRepository as:

@Repository
public interface PassengerRepository extends JpaRepository<Passenger, Long> {
    List<Passenger> findByFirstNameIgnoreCase(String firstName);
}

Here, the IgnoreCase keyword ensures that the query matches are case insensitive.

We can also test that out with the help of a JUnit test:

@Test
public void givenPassengers_whenMatchingIgnoreCase_thenExpectedReturned() {
    Passenger jill = Passenger.from("Jill", "Smith");
    Passenger eve = Passenger.from("Eve", "Jackson");
    Passenger fred = Passenger.from("Fred", "Bloggs");
    Passenger siya = Passenger.from("Siya", "Kolisi");
    Passenger ricki = Passenger.from("Ricki", "Bobbie");

    List<Passenger> passengers = repository.findByFirstNameIgnoreCase("FrED");

    assertThat(passengers, contains(fred));
    assertThat(passengers, not(contains(eve)));
    assertThat(passengers, not(contains(siya)));
    assertThat(passengers, not(contains(jill)));
    assertThat(passengers, not(contains(ricki)));
}

Despite having passed “FrED” as the argument, our returned list of passengers contains a Passenger with the firstName as “Fred”. Clearly, with the help of the IgnoreCase keyword, we have achieved a case insensitive match.

5. Conclusion

In this quick tutorial, we learned how to create a case insensitive query in a Spring Data repository.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW