How to pass parameter in HQL query

22.8k views Asked by At

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..!!

3

There are 3 answers

0
Anirudh Jadhav On BEST ANSWER

I is very simple to add parameter to an HQL

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

here i have hard coded 15 you can simply use variable instead of it

0
Jean Cedron On

Simple example:

Integer id = 1;
Query query = session.createQuery("from Employee e where e.idEmployee=:id");
query.setParameter("id", id);
0
Baba Fakruddin On

NAMEDPARAMETER

String hql = "from com.baba.app.Model.Employee where mngId=:id or mngName=:name";
            Query q = ses.createQuery(hql);
            q.setParameter("id", 121);
            q.setParameter("name", "baba");

PARAMETER

String hql = "from com.baba.app.Model.Employee where mngId= ? or mngName= ?";
            Query q = ses.createQuery(hql);
            q.setParameter(0, 121);
            q.setParameter(1, "baba");