Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate query: positioned parameter and named parameter

Tags:

java

hibernate

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?

like image 783
zhongshu Avatar asked Mar 20 '10 10:03

zhongshu


People also ask

What is named parameter in hibernate?

Named query parameters are tokens of the form :name in the query string. A value is bound to the integer parameter :foo by calling setParameter("foo", foo, Hibernate. INTEGER); for example. A name may appear multiple times in the query string.

How are named parameters specified in an HQL query?

It's use question mark (?) to define a named parameter, and you have to set your parameter according to the position sequence. See example… String hql = "from Stock s where s. stockCode = ? and s.

What is query uniqueResult () in hibernate?

uniqueResult() Convenience method to return a single instance that matches the query, or null if the query returns no results.

What is positional parameter in SQL?

A positional parameter is set by its index in the clause. A named parameter is set by its name. When you are setting the values, you might have the values in an array, in which case the positional form could me more useful.


1 Answers

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();
like image 101
Jeshurun Avatar answered Oct 21 '22 03:10

Jeshurun