skip to Main Content

I am planning to migrate spring boot from 2.7.1 to 3.2.0.

It’s my model class:

@Entity
@Table(name = "tutorials")
public class Tutorial {

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

    @Column(name = "title")
    private String title;

    @Column(name = "description")
    private String description;

    @Column(name = "published")
    private Boolean published;

    // setters and getters
}

And my repository:

public interface TutorialRepository extends JpaRepository<Tutorial, Long> {     
    @Query("select t from Tutorial t where t.published IS TRUE")
    List<Tutorial> getTutorial();
}

And My rest Controller:

@RestController
@RequestMapping("/api")
public class TutorialController {

@Autowired
TutorialRepository tutorialRepository;

@GetMapping("/tutorials")
public ResponseEntity<List<Tutorial>> getAllTutorials(@RequestParam(required = false) String title) {
    try {
        List<Tutorial> tutorials = new ArrayList<Tutorial>();

        if (title == null)
            tutorialRepository.findAll().forEach(tutorials::add);
        else
            tutorialRepository.findByTitleContaining(title).forEach(tutorials::add);

        if (tutorials.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }

        return new ResponseEntity<>(tutorials, HttpStatus.OK);
    } catch (Exception e) {
        return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
    }
}


@GetMapping("/tutorial/published")
public ResponseEntity<List<Tutorial>> findByTutorialPublished() {
    try {
        List<Tutorial> tutorials = tutorialRepository.getTutorial();
        tutorials.forEach(System.out::println);
        if (tutorials.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity<>(tutorials, HttpStatus.OK);
    } catch (Exception e) {
        return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

}

And for database configuration, I create application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost/testdb
spring.datasource.username=root
spring.datasource.password=root

When I check the api ‘http://localhost:8080/api/tutorial/published’, got 500 internal server error. Means, fails to execute JPA query with boolean comparison. But its working in spring boot 2.7.1.

I again tried using hibernate to 6.4.0 in pom.xml.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.0</version>
        <relativePath />
    </parent>
    <groupId>com.bezkoder</groupId>
    <artifactId>spring-mysql</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-mysql</name>
    <description>Demo project for Spring Boot Apis CRUD using Spring Data JPA</description>

<properties>
    <java.version>21</java.version>
    <hibernate.version>6.4.0.Final</hibernate.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

No hope.

In Eclipse IDE, I can see following warning: (no issues)

ANTLR Tool version 4.10.1 used for code generation does not match the current runtime version 4.13.0
ANTLR Runtime version 4.10.1 used for parser compilation does not match the current runtime version 4.13.0

But the error in eclipse while testing the api as follows:

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]

Please guide me to move forward with Spring boot 3.2.0 ?

2

Answers


  1. You can use JPA Query Methods, like in your case findByPublishedTrue() so that the method name will be translated into the JPA query.

    public interface TutorialRepository extends JpaRepository<Tutorial, Long> {     
        List<Tutorial> findByPublishedTrue();
    }
    

    Note: you don’t have to use @Query on top your method.

    If you want to know more about JPA Query Methods, check the official documentation here – https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html

    Login or Signup to reply.
  2. check https://docs.spring.io/spring-data/jpa/docs/current-SNAPSHOT/reference/html/#jpa.query-methods.query-creation for query syntax.
    The error you got "Bad JPQL grammar [select t from Tutorial t where t.published IS TRUE]" clearly says JPA query syntax is not correct.

    also <hibernate.version>6.4.0.Final</hibernate.version> is not coming default with spring boot 3.2.0 hence the warning is coming because of incorrect version used to compile and run the hibernate queries. remove it to use default hibernate version which comes with spring boot 3.2.0 to resolve this warning

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search