When I execute a query with a bind parameter, no results are returned. When I execute the same query hard-coding the value for the bind parameter, the query returns a result. I've tried this with both 3.1.3 and 3.2.0 CR2 and I get the same result. So for example, when I execute this:
Code:
User user = (User)session
.createSQLQuery("select * from users where id = 1")
.addEntity(User.class).uniqueResult();
The user object is populated with the data correctly. When I execute this statement:
Code:
User user = (User)session
.createSQLQuery("select * from users where id = :id")
.addEntity(User.class).setLong("id",1l)
.uniqueResult();
user is null. You can see from the hibernate debug output below, hibernate thinks no rows are returned. I've tried this with version 3.1.3 and 3.2.0 CR2. I've tried annotation-based configuration and xml-based configuration. I've tried using load and get, both return null. I've tried other datatypes, like a string. For example:
Code:
User user = (User)session
.createSQLQuery("select * from users where username = 'pbarry'")
.addEntity(User.class).uniqueResult();
That gives me data. Then this:
Code:
User user = (User)session
.createSQLQuery("select * from users where username = :username")
.addEntity(User.class).setString("username","pbarry")
.uniqueResult();
user is null. I've tried with a valid implementation for equals/hashcode in my User class and one with out. The results are pretty consistent, any time a bind parameter is involved, hibernate appears to correctly bind the right value, but then gets no results from the database. Anybody have any ideas to offer? Here are the rest of my configuration details:
Hibernate version: 3.2.0 CR2 and 3.1.3
Mapping documents:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="my.package.model.User" table="users">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username"/>
<property name="password"/>
<property name="firstName"/>
<property name="lastName"/>
<property name="email"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
User user = (User)session
.createSQLQuery("select * from users where id = :id")
.addEntity(User.class).setLong("id",1l)
.uniqueResult();
Full stack trace of any exception that occurs:No exception, returns null
Name and version of the database you are using:MySQL 4.1.13 & MySQL Connector/J 3.1.7 on Ubuntu Dapper 6.06 with Sun JDK 1.5.0_06
The generated SQL (show_sql=true):See hibernate log below
Debug level Hibernate log excerpt:Code:
10:38:55,588 DEBUG org.hibernate.SQL:346 -
select
*
from
users
where
id = ?
10:38:55,591 DEBUG org.hibernate.jdbc.AbstractBatcher:424 - preparing statement
10:38:55,603 DEBUG org.hibernate.loader.Loader:1742 - bindNamedParameters() 1 -> id [1]
10:38:55,605 DEBUG org.hibernate.type.LongType:80 - binding '1' to parameter: 1
10:38:55,606 DEBUG org.hibernate.jdbc.AbstractBatcher:327 - about to open ResultSet (open ResultSets: 0, globally: 0)
10:38:55,608 DEBUG org.hibernate.loader.Loader:682 - processing result set
10:38:55,609 DEBUG org.hibernate.loader.Loader:709 - done processing result set (0 rows)
10:38:55,610 DEBUG org.hibernate.jdbc.AbstractBatcher:334 - about to close ResultSet (open ResultSets: 1, globally: 1)
10:38:55,613 DEBUG org.hibernate.jdbc.AbstractBatcher:319 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
10:38:55,614 DEBUG org.hibernate.jdbc.AbstractBatcher:470 - closing statement