Hibernate - can I use named and positional parameters?

I have a query string line by line:

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 up my positional parameter, the query is executed as usual, but there is no result:

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

It is not right. If I changed my positional to named:

 query.setParameter(firstname, "firstname1010"); 

Then my query returns the correct results.

Without going into an intricate explanation of why I am doing this, I would like to know whether mixing of the two types should be supported or not? I am using hibernate 3.6.3. Final

+7
source share
1 answer

In class level documents org.hibernate.Query :

You cannot mix and match JDBC style parameters and named parameters in the same query.

So, the behavior you see is fully expected.

+5
source

All Articles