如何在HQL查询中传递参数

时间:2015-06-11 05:21:17

标签: sql hibernate hql

在我的HQL查询下面找到

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

此查询工作正常,但在此,我想传递userId的值,但我无法弄清楚如何执行此操作。 请帮助.. !! 在此先感谢.. !!

3 个答案:

答案 0 :(得分:2)

简单示例:

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

答案 1 :(得分:2)

将参数添加到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);

这里我有硬编码15你可以简单地使用变量代替它

答案 2 :(得分:0)

NAMEDPARAMETER

<main> <section class="introduction"> <img src="https://svgur.com/i/ZTL.svg" class="introduction__up-shape" alt=""> <div class="introduction__content"> <h2 class="introduction__header secondary-header">About me</h2> <p class="introduction__paragraph"> Lorem ipsum, dolor sit amet consectetur adipisicing elit. Nihil, placeat deserunt. Quae dolorum deserunt assumenda expedita. Excepturi eveniet iusto a. </p> <p class="introduction__paragraph"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Quibusdam, corrupti veniam cupiditate blanditiis ipsam enim fuga odio? Ut vel necessitatibus voluptas. Accusantium non velit nam. </p> <p class="introduction__paragraph"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Accusantium sed quasi beatae rerum id reiciendis eaque, ut doloremque, iusto amet non necessitatibus debitis commodi pariatur voluptates delectus libero deleniti consectetur! </p> </div> <img src="https://svgur.com/i/ZQw.svg" alt=""> </section> </main>

参数

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");