Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter in HQL query

Tags:

sql

hibernate

hql

find below my HQL query

 Query query = session.createQuery("select u from UserLog u where u.userLogSerialno = " + "(select max(uu.userLogSerialno) from UserLog uu where uu.userId = u.userId)");

This query is working fine but in this, I want to pass the value of userId but I am not able to figure out how to do this. Kindly Help..!! Thanks in Advance..!!

like image 365
Chirag Kawariya Avatar asked Jun 11 '15 05:06

Chirag Kawariya


People also ask

How are parameters specified in an HQL query?

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.

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.

Is HQL faster than SQL?

Main Differences Between SQL and HQLSQL is usually faster than the non-native HQL, however, by setting the correct cache size of the query plan, HQL can be made to operate as fast as SQL.


1 Answers

Simple example:

Integer id = 1;
Query query = session.createQuery("from Employee e where e.idEmployee=:id");
query.setParameter("id", id);
like image 171
Jean Cedron Avatar answered Sep 20 '22 11:09

Jean Cedron