Skip to content

Why Does Spring Data JPA Validate Queries at Startup and Throw IllegalArgumentException

Problem

When I started my Spring Boot application, I got this error:

Startup error
Caused by: java.lang.IllegalArgumentException:
Validation failed for query for method
public abstract java.util.List com.example.repository.UserRepository.findByFirstName(java.lang.String)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:103)
at org.springframework.data.jpa.repository.query.JpaQueryMethod$JpaQueryFactory.fromQueryAnnotation(JpaQueryMethod.java:254)

My application failed to start completely. I couldn’t understand why Spring was validating my queries at startup instead of when they were actually executed.

Environment

  • Spring Boot 3.2.x
  • Spring Data JPA 3.2.x
  • Java 21
  • Hibernate as JPA provider

What happened?

I had defined a simple repository with a custom query:

UserRepository.java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
@Query("SELECT u FROM User u WHERE u.firstName = :name")
List<User> findByFirstName(@Param("name") String name);
}

The entity looked like this:

User.java
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
}

When I ran the application, it failed during the ApplicationContext initialization phase, before any of my code even executed.

The reason

Spring Data JPA validates all @Query annotations at application startup. This is a fail-fast mechanism designed to catch errors early.

Here’s what happens:

Validation flow
ApplicationContext starts
Spring Data scans repository interfaces
For each @Query annotation found
SimpleJpaQuery.validateQuery() is called
JPA provider attempts to parse JPQL
If parsing fails → IllegalArgumentException

The validation happens in org.springframework.data.jpa.repository.query.SimpleJpaQuery. The validateQuery() method is invoked during ApplicationContext startup for every @Query annotation.

This follows the JPA specification requirement that EntityManager.createQuery() must throw IllegalArgumentException for invalid query strings.

Why this is a good thing

At first, I thought this was annoying. But I realized this fail-fast approach provides a protective layer:

  1. Catches errors before production - If my JPQL has syntax errors, I find out immediately, not when a user triggers a specific code path

  2. Saves debugging time - The error happens at startup with a clear stack trace, not buried in logs during runtime

  3. Validates against entity model - The JPA provider checks that entity names and field names exist

What to check when this error occurs

When you see “Validation failed for query for method”, check these common causes:

CauseExampleFix
Wrong entity nameSELECT u FROM Users uUse User (entity class name), not users (table name)
Wrong field nameu.first_nameUse u.firstName (Java field name), not database column
Reserved keywordu.groupEscape in @Column(name = "\group`”)`
SQL instead of JPQLSELECT * FROM usersAdd nativeQuery = true

Summary

In this post, I explained why Spring Data JPA validates queries at startup. The key point is that SimpleJpaQuery.validateQuery() runs during ApplicationContext initialization as a fail-fast mechanism. This catches JPQL errors before your application handles any requests.

When you see the “Validation failed for query” error, check your JPQL syntax: entity names, field names, reserved keywords, and whether you need nativeQuery = true.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments