How to use Spring Boot 3.2.0 with MySQL database and JpaRepository @Query has boolean comparison – Mysql

by
Ali Hasan
java jpa libmysqlclient spring spring-boot

Quick Fix: Refer to the Spring Data JPA documentation for query syntax and ensure that your JPQL query is correct. Additionally, update the hibernate version in your configuration to the default version that comes with Spring Boot 3.2.0 to resolve the warning.

The Problem:

A user is encountering an internal server error when trying to execute a JPA query with a boolean comparison in Spring Boot 3.2.0, using MySQL as the database. The code was working in Spring Boot 2.7.1, but now fails with the error "org.springframework.dao.InvalidDataAccessApiUsageException: org.springframework.data.jpa.repository.query.BadJpqlGrammarException: Line 1:44 mismatched input ‘IS’ expecting {, ‘,’, ‘+’, ‘-‘, ‘/’, ‘||’, ‘[‘, ‘.’, AND, ‘*’, BY, DAY, EPOCH, EXCEPT, GROUP, HOUR, INTERSECT, MINUTE, MONTH, NANOSECOND, OR, ORDER, QUARTER, SECOND, UNION, WEEK, YEAR}; Bad JPQL grammar [select t from Tutorial t where t.published IS TRUE]". The query attempts to retrieve all published tutorials from the database.

The Solutions:

Solution 1: Check the JPA query syntax.

The error “org.springframework.dao.InvalidDataAccessApiUsageException: org.springframework.data.jpa.repository.query.BadJpqlGrammarException: Line 1:44 mismatched input ‘IS’ expecting {<EOF>, ‘,’, ‘+’, ‘-‘, ‘/’, ‘||’, ‘[‘, ‘.’, AND, ‘*’, BY, DAY, EPOCH, EXCEPT, GROUP, HOUR, INTERSECT, MINUTE, MONTH, NANOSECOND, OR, ORDER, QUARTER, SECOND, UNION, WEEK, YEAR}; Bad JPQL grammar [select t from Tutorial t where t.published IS TRUE]” indicates that the JPA query syntax is incorrect.

To fix this issue, refer to the Spring Data JPA documentation for the correct syntax for boolean comparisons in JPA queries: https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#jpa.query-methods.query-creation.

Typically, for boolean comparisons, you should use the = operator instead of the IS operator. So, in your case, you should change the query to "select t from Tutorial t where t.published = true".

Solution 2: Remove the hibernate.version property from the pom.xml file.

The property <hibernate.version>6.4.0.Final</hibernate.version> is not included by default in Spring Boot 3.2.0. This is why you are getting the warning “ANTLR Tool version 4.10.1 used for code generation does not match the current runtime version 4.13.0”.

To resolve this warning, remove the <hibernate.version> property from the pom.xml file. This will allow Spring Boot to use the default Hibernate version that is compatible with Spring Boot 3.2.0.

Solution 3: Ensure that the MySQL database is properly configured.

Make sure that the MySQL database is running and that the database connection details in the application.properties file are correct. You can also try restarting the MySQL database and the Spring Boot application to see if that resolves the issue.

Q&A

How to use JPA Query Methods in Spring Data?

Use findByPublishedTrue() instead of @Query.

Why am I getting ‘Bad JPQL grammar’ error?

How to remove Antlr warning?

Remove <hibernate.version>6.4.0.Final</hibernate.version> from pom.xml.