简体   繁体   中英

Hibernate query: positioned parameter and named parameter

There are two types of query parameters binding in the 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. Please check his answer below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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