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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With