-->
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: Simple query not working
PostPosted: Fri Oct 22, 2004 12:38 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
Very weird. I have a table
+---------+---------+--------------+
| ITEM_ID | version | name |
+---------+---------+--------------+
| 1 | 11 | yyyyyyyyy |
| 2 | 0 | another item |
+---------+---------+--------------+

When I run the SQL generated by hibernate, I get all the entries, but when I run the simple HQL query of "from NamedItem", I get two namedItems both representing row 1 instead of two objects representing each row.

Hibernate version:
2.1
Code between sessionFactory.openSession() and session.close():
Code:
      Query byName = session.createQuery("from NamedItem");
      List l = byName.list();
      for(int i = 0; i < l.size(); i++) {
         NamedItem item = (NamedItem)l.get(0);
         System.out.println("item["+i+"]="+item);
      }

Mapping documents:
Code:
<hibernate-mapping>
    <class
        name="biz.xsoftware.model.chap5.NamedItem"
        table="CHAP5_ITEM"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="ITEM_ID"
            type="java.lang.Long"
        >
            <generator class="increment">
            </generator>
        </id>

        <version
            name="version"
            type="int"
            column="version"
            access="property"
            unsaved-value="undefined"
        />

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            access="property"
            column="name"
        />

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

    </class>

</hibernate-mapping>

Full stack trace of any exception that occurs:

Name and version of the database you are using:
MySQL
The generated SQL (show_sql=true):
Hibernate: select nameditem0_.ITEM_ID as ITEM_ID, nameditem0_.version as version, nameditem0_.name as name from CHAP5_ITEM nameditem0_
item[0]=(yyyyyyyyy,1,11)
item[1]=(yyyyyyyyy,1,11)
Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: Re: Simple query not working
PostPosted: Fri Oct 22, 2004 2:36 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
deanhiller wrote:
Very weird.
Code:
      Query byName = session.createQuery("from NamedItem");
      List l = byName.list();
      for(int i = 0; i < l.size(); i++) {
         NamedItem item = (NamedItem)l.get(0);
         System.out.println("item["+i+"]="+item);
      }

[/b]


not weird, your loop is flawed.
get(i) instead of get(0)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 23, 2004 10:19 am 
Regular
Regular

Joined: Thu Sep 16, 2004 4:56 pm
Posts: 80
yes, I actually found that out and thought I posted a never mind message, but I guess I forgot to click submit or something. I was pretty tired when working on that code. decided to go to bed once I found that mistake.
thanks for the help though,
dean


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.