Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - can I mix named and positional parameters?

I have a queryString along the lines of:

session.createSQLQuery("SELECT C.FIRSTNAME AS firstName, C.LASTNAME as lastName FROM ADDRESSBOOK_CONTACT AS C WHERE C.ADDRESSBOOK_ID = :addressbookId AND firstName = ?");

When setting my positional parameter, the query is run as normal but there is no result:

query.setParameter(0, "firstname1010");
query.setParameter("addressbookId", addressbook.getId());

Which is wrong. If I change my positional to named:

query.setParameter(firstname, "firstname1010");

Then my query returns the correct results.

Without going into a convoluted explanation as to why I am doing this, I would like to know if mixing the two types should be supported or not? I am using hibernate 3.6.3.Final

like image 769
mogronalol Avatar asked Jun 28 '12 10:06

mogronalol


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.

Does Java support positional parameters?

You may use positional parameters instead of named parameters in queries. Positional parameters are prefixed with a question mark (?) followed the numeric position of the parameter in the query.

What is positional parameter and named parameter in java?

A positional parameter is linked by its position. Positional parameters must be specified in the order in which they appear. Named parameters are specified by assigning values to their names. For an attribute, you can also create named parameters. named parameters can be assigned initial values by using their names.


1 Answers

From the Class level docs on org.hibernate.Query:

You may not mix and match JDBC-style parameters and named parameters in the same query.

So the behaviour you're seeing is completely expected.

like image 77
Alex Barnes Avatar answered Oct 30 '22 06:10

Alex Barnes