Jpa-style positional param was not an integral ordinal

Explanation of “jpa-style positional param was not an integral ordinal” error message

This error message usually occurs when using positional parameters in a JPA query and not providing a valid integral ordinal for the parameter. Let’s break down the error message and explain it in more detail.

  • “jpa-style positional param”: This refers to the way we define parameters in a JPA query using a positional approach, where a question mark (?) is used to represent parameters in the query string.
  • “was not an integral ordinal”: An integral ordinal refers to a positive integer value that represents the position of the parameter in the query string.

Here’s an example to illustrate the error. Assume we have the following JPA query:

    
      String queryString = "SELECT e FROM Employee e WHERE e.salary > ? AND e.department = ?";
      Query query = entityManager.createQuery(queryString);
      query.setParameter(1, 5000); // Incorrectly specifying parameter position as 1
      query.setParameter(2, "IT");
    
  

In the above example, the error occurs because we provided the parameter position as 1, which is incorrect. The first parameter should have an ordinal value of 0 since positional parameters start with 0. So the corrected code will be:

    
      String queryString = "SELECT e FROM Employee e WHERE e.salary > ? AND e.department = ?";
      Query query = entityManager.createQuery(queryString);
      query.setParameter(0, 5000); // Correctly specifying parameter position as 0
      query.setParameter(1, "IT");
    
  

In this corrected code, we set the first parameter position as 0 and the second parameter position as 1, which aligns with the correct integral ordinals.

Related Post

Leave a comment