在Hibernate查询上链接setParameter [英] Chaining setParameter on Hibernate Query

查看:96
本文介绍了在Hibernate查询上链接setParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所见,我有两个命名参数,一个由setParameterList()设置,另一个由setParmeter()设置.问题是列表未排序.当我显式设置order字段时,它可以正常工作,但是将相同的字符串传递到该方法中则不起作用.是setParameter和setParameterList无法链接的吗?他们都返回查询,我不明白为什么不这样做.我想念什么?

As you can see I have two named parameters, one being set by setParameterList() and one being set by setParmeter(). The problem is the List is not being ordered. When I set the order field explicitly it works fine, but the same string is being passed into the method it doesn't work. Is it that setParameter and and setParameterList can't be chained? They both return a query do I don't see why not. What am I missing?

public List<Subject> getSubjectsByMedium(String orda, Medium... medium) {
    List<Subject> subjects = currentSession().createQuery("from Subject where medium in(:medium) order by :orda").setParameterList("medium", medium).setParameter("orda", orda).list();
    return Subjects;
}

推荐答案

不,这不是方法链接的问题.问题是您不能使用命名参数在HQL(或SQL)查询中设置ORDER.

No, it's not a problem of method chaining. The problem is you can't use named parameters to set the ORDER in an HQL (or SQL) query.

您需要分别构建查询字符串,然后在创建的Query对象上设置:medium命名参数.

You'll need to build the query String separately and then set the :medium named parameter on the created Query object.

String query = "from Subject where medium in(:medium) order by " + orda;

这确实可能使您容易受到SQL注入的攻击.

This does possibly leave you vulnerable to SQL injection.

这篇关于在Hibernate查询上链接setParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆