java Hibernate 查询:定位参数和命名参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2482733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:23:51  来源:igfitidea点击:

Hibernate query: positioned parameter and named parameter

javahibernate

提问by zhongshu

There are two types of query parameters binding in the Hibernate Query. One is positioned parameter and another one is named parameter.

Hibernate Query 中有两种类型的查询参数绑定。一个是定位参数,另一个是命名参数。

Can I use these two parameters in one Query?

我可以在一个查询中使用这两个参数吗?

采纳答案by marcosbeirigo

I Don't think so, if you try it, hibernate you give you the following error:

我不这么认为,如果你尝试一下,休眠你会给你以下错误:

org.hibernate.hql.ast.QuerySyntaxException: cannot define positional parameter after any named parameters have been defined

Why would you want to do that?

你为什么想这么做?

回答by Jeshurun

Sure you can, as long as you make sure all positional parameters precede any named parameters. Heres an example:

当然可以,只要您确保所有位置参数都位于任何命名参数之前。这是一个例子:

    Query q =session.createQuery("select u from User u where u.location=? and u.id in (:user_ids)");
    q.setParameter(0, location);
    q.setParameterList("user_ids", userIds);
    return q.list();