How to pass parameter in HQL query

感情迁移 提交于 2019-12-20 02:56:48

问题


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


回答1:


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




回答2:


Simple example:

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


来源:https://stackoverflow.com/questions/30772171/how-to-pass-parameter-in-hql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!