简体   繁体   English

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

[英]Hibernate query: positioned parameter and named parameter

There are two types of query parameters binding in the Hibernate Query. Hibernate Query 中有两种类型的查询参数绑定。 One is positioned parameter and another one is named parameter.一个是定位参数,另一个是命名参数。

Can I use these two parameters in one Query?我可以在一个查询中使用这两个参数吗?

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

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?你为什么想这么做?

Edit: Jeshurun offers a much better solution.编辑:Jeshurun 提供了一个更好的解决方案。 Please check his answer below.请在下面查看他的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM