-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Can't get hibernate queries with bind parameters to work
PostPosted: Thu Aug 17, 2006 10:52 am 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 1:34 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
This code all works fine if I use an HSQLDB database instead of MySQL. I was using the MySQLDialect with MyISAM table types. I want InnoDB tables anyway, so I switched my tables to InnoDB and switched the dialect to MySQLInnoDBDialect, but the problem still persists. I've also tried using MySQL Connector/J 3.1.13, but that's no help either.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 17, 2006 2:03 pm 
Beginner
Beginner

Joined: Fri Jul 30, 2004 2:53 pm
Posts: 33
Location: Washington, DC
Guess it helps to read the FAQ:

from http://www.hibernate.org/120.html

Quote:
The MySQL Server 4.1.12 RPM delivered with Mandriva Linux 2006 breaks prepared statements. This can be solved by either adding "useServerPrepStmts=false" to the MySQL connection string in the Hibernate configuration file or by using an unbroken original MySQL Server package. http://bugs.mysql.com/bug.php?id=14910


I put userServerPrepStmts=false in my connect string and everything is working.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.