Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordinal Parameter Not bound : 2 in @Query annotation

I am getting following error when I try to run this query.

org.hibernate.QueryException: Ordinal parameter not bound : 2",

@Query(value = "SELECT amu " +
                   "FROM Upgrade amu " +
                    "INNER JOIN FETCH amu.visibility v " +
                   " WHERE v IN ?2 " +
                    "AND amu.id= '?1' "         
   )
Optional<Upgrade> myFindMethod(final String uid, final String cid);

If I change " WHERE v IN ?2 " with " WHERE v IN ?2 OR v IN ?1 ", then it works. I have no idea why it does not work. Any idea?

Note: Visibility is type of Set<String> in the Upgrade class.

like image 811
user1474111 Avatar asked Nov 20 '19 16:11

user1474111


2 Answers

To add to Jens' answer: be aware of the spaces!

For example, this will throw the error:

"WHERE v IN ?2" + "AND amu.id= ?1 "

And this will work fine:

"WHERE v IN ?2 " + "AND amu.id= ?1 "

It's a minute difference that's easy to overlook.

like image 195
Ondrej Sotolar Avatar answered Nov 20 '22 05:11

Ondrej Sotolar


The problem is that you have only one bind parameter declared in the query but have actually two parameters in the method.

This is because in "AND amu.id= '?1' " what looks like a bind parameter is actually a string literal due to the enclosing quotes. If you want that to be handled as a bind parameter remove the quotes: "AND amu.id= ?1 "

like image 15
Jens Schauder Avatar answered Nov 20 '22 05:11

Jens Schauder