-->
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.  [ 5 posts ] 
Author Message
 Post subject: PreparedStatement bug?
PostPosted: Wed Feb 22, 2006 5:19 am 
Newbie

Joined: Thu May 26, 2005 6:24 am
Posts: 6
Hi all,

When I ran the sql query generated by Hibernate at MySQL Command Line Client, I got 1 row of result.. But not in my app that uses Hibernate..

Then I wrote a simple Java App to connect to the DB directly. If I used PreparedStatement to execute the query, I didn't get any row, but when I used Statement to execute the query, it returned me with 1 row of result.

Here is the code that I wrote to execute the query:

Code:
public static void main(String[] args) throws Exception {
Connection result = null;
       try {
          Class.forName("com.mysql.jdbc.Driver").newInstance();
        }
        catch (Exception ex){
           System.err.println("Check classpath. Cannot load db driver: com.mysql.jdbc.Driver");
        }

        try {
          result = DriverManager.getConnection("jdbc:mysql://127.0.0.1/zzz", "xxx", "yyy");
        }
        catch (SQLException e){
           System.err.println( "Driver loaded, but cannot connect to db: " + "jdbc:mysql://127.0.0.1/zzz");
        }
        
        String sql ="select provider0_.ID as ID0_, case when provider0__2_.ID is not null then 1 " +
                     "when provider0_.ID is not null then 0 end as clazz_0_, provider0__1_.VERSION as "+
                       "VERSION6_0_, provider0__1_.USERNAME as USERNAME6_0_, provider0__1_.PASSWORD as PASSWORD6_0_, provider0__2_.ADDRESS as ADDRESS8_0_, provider0__2_.EMAIL as EMAIL8_0_, provider0__2_.FULLNAME as FULLNAME8_0_, provider0__2_.PHONE as PHONE8_0_ from PROVIDER provider0_ inner join USER provider0__1_ on provider0_.ID=provider0__1_.ID left outer join PODCAST_PROVIDER provider0__2_ on provider0_.ID=provider0__2_.ID where provider0_.ID=?";
        
        System.out.println("PreparedStatement");
       
        PreparedStatement pstmt = result.prepareStatement(sql);
        pstmt.setLong(1,3);
        System.out.println("pstmt["+pstmt+"]");
        ResultSet rs = pstmt.executeQuery();
          while(rs.next()) {
             System.out.println("YES");
          }
       
        String anotherSql = "select provider0_.ID as ID0_, case when provider0__2_.ID is not null then 1 when provider0_.ID is not null then 0 end as clazz_0_, provider0__1_.VERSION as VERSION6_0_, provider0__1_.USERNAME as USERNAME6_0_, provider0__1_.PASSWORD as PASSWORD6_0_, provider0__2_.ADDRESS as ADDRESS8_0_, provider0__2_.EMAIL as EMAIL8_0_, provider0__2_.FULLNAME as FULLNAME8_0_, provider0__2_.PHONE as PHONE8_0_ from PROVIDER provider0_ inner join USER provider0__1_ on provider0_.ID=provider0__1_.ID left outer join PODCAST_PROVIDER provider0__2_ on provider0_.ID=provider0__2_.ID where provider0_.ID=3";
          
        System.out.println("End pstmt");
        System.out.println("Statement");
        Statement stmt = result.createStatement();
        String newSQL = sql.substring(0,sql.length()-1);
        newSQL +=3;
        rs = stmt.executeQuery(newSQL);
        while(rs.next()) {
          System.out.println("YES2");
        }
        System.out.println("End stmt");
        
        System.out.println("New Statement");
        stmt = result.createStatement();
       
        rs = stmt.executeQuery(anotherSql);
        while(rs.next()) {
          System.out.println("YES3");
        }
}

The following is the result of running the above code :
Code:
PreparedStatement
pstmt[com.mysql.jdbc.ServerPreparedStatement[1] - select provider0_.ID as ID0_, case when provider0__2_.ID is not null then 1 when provider0_.ID is not null then 0 end as clazz_0_, provider0__1_.VERSION as VERSION6_0_, provider0__1_.USERNAME as USERNAME6_0_, provider0__1_.PASSWORD as PASSWORD6_0_, provider0__2_.ADDRESS as ADDRESS8_0_, provider0__2_.EMAIL as EMAIL8_0_, provider0__2_.FULLNAME as FULLNAME8_0_, provider0__2_.PHONE as PHONE8_0_ from PROVIDER provider0_ inner join USER provider0__1_ on provider0_.ID=provider0__1_.ID left outer join PODCAST_PROVIDER provider0__2_ on provider0_.ID=provider0__2_.ID where provider0_.ID=3]
End pstmt
Statement
YES2
End stmt
New Statement
YES3


Appreciate any advice you can give on this matter. Thank you in advance

Best Regards,
Antonius Ng


Hibernate version:
2.1.8

Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
<class
name="com.n2n.user.User"
table="USER"
>

<id
name="id"
column="ID"
type="long"
unsaved-value="null"
>
<generator class="native">

</generator>
</id>

<version
name="version"
column="VERSION"
type="int"
/>

<property
name="username"
type="java.lang.String"
update="true"
insert="true"
column="USERNAME"
length="16"
not-null="true"
unique="true"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="PASSWORD"
length="16"
not-null="true"
/>

<set
name="roles"
table="USER_ROLE"
lazy="true"
cascade="none"
sort="unsorted"
>

<key
column="USER_ID"
>
</key>

<many-to-many
class="com.n2n.user.Role"
column="ROLE_ID"
outer-join="auto"
/>

</set>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-User.xml
containing the additional properties and place it in your merge dir.
-->

<joined-subclass
name="com.n2n.cms.user.Provider"
table="PROVIDER"
>
<key
column="ID"
/>

<set
name="contents"
lazy="true"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
>

<key
column="PROVIDER_ID"
>
</key>

<one-to-many
class="com.n2n.cms.content.Content"
/>

</set>

<joined-subclass
name="com.n2n.cms.podcast.user.PodcastProvider"
table="PODCAST_PROVIDER"
>
<key
column="ID"
/>
<property
name="address"
type="java.lang.String"
update="true"
insert="true"
column="ADDRESS"
not-null="false"
/>

<property
name="email"
type="java.lang.String"
update="true"
insert="true"
column="EMAIL"
not-null="true"
/>

<property
name="fullname"
type="java.lang.String"
update="true"
insert="true"
column="FULLNAME"
not-null="true"
/>

<property
name="phone"
type="java.lang.String"
update="true"
insert="true"
column="PHONE"
not-null="true"
/>

</joined-subclass>

</joined-subclass>

</class>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Code:
String queryString="from Channel channel where channel.provider.id = :provider";
Map params = new HashMap();
      params.put("provider",provider.getId());
      try {
         
               Query query = session.createQuery(queryString);

            Iterator keys = params.keySet().iterator();
            while(keys.hasNext()){
                String key = (String) keys.next();
                Object param = params.get(key);
                if(param instanceof Collection)
                    query.setParameterList(key, (Collection) param);
                else
                    query.setParameter(key, param);
            }
            ls = query.list();



Full stack trace of any exception that occurs:
Caused by: net.sf.hibernate.UnresolvableObjectException: No row with the given i
dentifier exists: 3, of class: com.n2n.cms.user.Provider
at net.sf.hibernate.UnresolvableObjectException.throwIfNull(Unresolvable
ObjectException.java:38)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1964)

at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.j
ava:69)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:20
8)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2
222)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loade
r.java:326)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:316)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections
(Loader.java:141)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1097)
at net.sf.hibernate.loader.Loader.list(Loader.java:1088)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:49)
at com.n2n.entity.hibernate.HibernateEntityDAO.useQuery(HibernateEntityD
AO.java:315)
... 31 more

Name and version of the database you are using:
MySQL 4.1

The generated SQL (show_sql=true):
select provider0_.ID as ID0_, case when provider0__2_.ID is not null then 1 when
provider0_.ID is not null then 0 end as clazz_0_, provider0__1_.VERSION
as VERSION6_0_, provider0__1_.USERNAME as USERNAME6_0_, provider0__1_.PASSWORD
as PASSWORD6_0_, provider0__2_.ADDRESS as ADDRESS8_0_, provider0__2_.EMAIL as
EMAIL8_0_, provider0__2_.FULLNAME as FULLNAME8_0_, provider0__2_.PHONE as
PHONE8_0_ from PROVIDER provider0_ inner join USER provider0__1_ on
provider0_.ID=provider0__1_.ID left outer join PODCAST_PROVIDER provider0__2_ on
provider0_.ID=provider0__2_.ID where provider0_.ID=?;

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 9:10 am 
Senior
Senior

Joined: Tue May 10, 2005 9:00 am
Posts: 125
Could it be the table at the server has String as id instead of something convertable to java.lang.Long?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 10:55 pm 
Newbie

Joined: Thu May 26, 2005 6:24 am
Posts: 6
All the tables are auto-generated by using hibernate schema-export. The ID fields in the tables USER, PROVIDER and PODCAST_PROVIDER are of type bigint(20).

Code:
mysql> desc provider
    -> ;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| ID    | bigint(20) |      | PRI | 0       |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.09 sec)

mysql> desc podcast_provider;
+----------+--------------+------+-----+---------+-------+
| Field    | Type         | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| ID       | bigint(20)   |      | PRI | 0       |       |
| ADDRESS  | varchar(255) | YES  |     | NULL    |       |
| EMAIL    | varchar(255) |      |     |         |       |
| FULLNAME | varchar(255) |      |     |         |       |
| PHONE    | varchar(255) |      |     |         |       |
+----------+--------------+------+-----+---------+-------+
5 rows in set (0.03 sec)

mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| ID       | bigint(20)  |      | PRI | NULL    | auto_increment |
| VERSION  | int(11)     |      |     | 0       |                |
| USERNAME | varchar(16) |      | UNI |         |                |
| PASSWORD | varchar(16) |      |     |         |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)


Top
 Profile  
 
 Post subject: Re: PreparedStatement bug
PostPosted: Thu Feb 23, 2006 12:15 am 
Newbie

Joined: Thu Feb 23, 2006 12:12 am
Posts: 3
You should be running at least MySQL-4.1.12 because of the following bug with prepared statements which you're probably running into:

http://bugs.mysql.com/9096

http://bugs.mysql.com/9777

4.1.18 is the current version in the 4.1 series.

-Mark


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 3:36 am 
Newbie

Joined: Thu May 26, 2005 6:24 am
Posts: 6
Thanks Mark,

I have upgraded the server to MySQL5. Now it is working fine.

Best Regards,
Antonius Ng


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.