Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate HQL查询_Hibernate_Hql - Fatal编程技术网

Hibernate HQL查询

Hibernate HQL查询,hibernate,hql,Hibernate,Hql,我正在尝试使用多个where参数创建HQL查询,如 result = sessionFactory.getCurrentSession().createQuery("from County where " + [0].property + "=?"+","+ c[1].property + "=?") .setParameter(0,c[0]。值) .setParameter(1,c[1].value).list() 我没有这样做,而是尝试创建一个可以处理任意数量的参数的查询,如 for(Pa

我正在尝试使用多个where参数创建HQL查询,如

result = sessionFactory.getCurrentSession().createQuery("from County where " + [0].property + "=?"+","+ c[1].property + "=?")
.setParameter(0,c[0]。值)
.setParameter(1,c[1].value).list()

我没有这样做,而是尝试创建一个可以处理任意数量的参数的查询,如

for(Params c:parms){`enter code here`
    queryString+= c.property +" = "+c.value+",";
    }
result = (State) sessionFactory.getCurrentSession()
                .createQuery("from County where " +queryString)
                .list().get(0);
那里的查询看起来是正确的,但它说“无法执行查询”

a)为什么您放弃使用预先准备好的语句,就像使用固定大小的参数一样

b) 你为什么要给c.property+“=”+c.value+“,”到“结果”而不是查询字符串

c) 那个逗号在上面做什么?不应该是“和”或“或”吗

你在评论你的答案吗

“a)使用多个PAM而不是固定大小”

String whereClause=新字符串();
用于(参数p:Params){
if(whereClause.isEmpty())
where子句=“where”;
其他的
其中,第+=“和”;
其中,子句+=p.property+“=?”;
}
Query Query=sessionFactory.getCurrentSession().createQuery(“来自县”+whereClause);
对于(int i=0;ia)使用多个PAM而不是固定大小b)它应该是queryString c),即使对于使用上述代码失败的地方,我只有一个参数!
String whereClause = new String();
for (Params p : params) {
    if (whereClause.isEmpty())
        whereClause = " where ";
    else
        whereClause += " and ";
    whereClause += p.property + " = ? ";
}
Query query = sessionFactory.getCurrentSession().createQuery("from County " + whereClause);
for (int i = 0; i<params.size(); i++) {
    query.setParameter(i, params[i].value);
}
result = (State) query.list().get(0);