Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to pass positional parameters into HQL query

Whilst trying to create an API for my app I tried to make a query that takes a value passed in and then returns the response from the db.

@Query(value = "SELECT u FROM User u WHERE u.userID = ?")
User getUserById(String id);

I have created queries this way in other projects but cannot figure out why I get the following error on this project

JDBC style parameters (?) are not supported for JPA queries.
like image 428
Richard Payne Avatar asked Apr 16 '18 19:04

Richard Payne


People also ask

How are positional parameters specified in an HQL query?

Positional parameters It's use question mark (?) to define a named parameter, and you have to set your parameter according to the position sequence. See example… String hql = "from Stock s where s. stockCode = ? and s.

How to use named parameters in hibernate?

Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling setParameter("foo", foo, Hibernate. INTEGER); for example. A name may appear multiple times in the query string.

Which of the following is used to bind the parameter in HQL?

Query objects use SQL or Hibernate Query Language (HQL) string to retrieve data from the database and create objects. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query.


2 Answers

have you tried this :

@Query(value = "SELECT u FROM User u WHERE u.userID  = :id")
User getUserById(String id);
like image 86
Elarbi Mohamed Aymen Avatar answered Oct 07 '22 00:10

Elarbi Mohamed Aymen


Encounter the same problem in HQL, in XML approach with positional Parameter

Exception: Legacy-style query parameters (?) are no longer supported

enter image description here

HQL Query

String hql = "from Customer cust where cust.city=?";

Solution: Change the hql query as below

String hql = "from Customer cust where cust.city=?0";

And here we go!!

like image 20
Kms Avatar answered Oct 06 '22 23:10

Kms