-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate HQL returns same objects.
PostPosted: Wed Nov 12, 2008 12:30 pm 
Newbie

Joined: Wed Nov 12, 2008 12:16 pm
Posts: 15
For some reason when i run my createQuery from the Session object that is initialized, it returns the correct number of objects, but the objects are duplicated. It seems that hibernate pulls the first row it finds containing my first column value.

Example:

DB Table

ROW_1 ROW_2 ROW_3 ROW_4
1234 abcd 10-08-2008 dev
1234 efgh 11-08-2008 dev
1234 ijkl 12-08-2008 dev
1234 mnop 1-08-2009 dev

if this were the data contained in my table and i had an HQL like this:

String SQL_STRING = "FROM UnitData data WHERE data.row4 = '" + row4 + "' ORDER BY data.row1 asc";

I would get 4 UnitData objects all with the same data:
1234 abcd 10-08-2008 dev

Any ideas as to why that happens?

_________________
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 4:32 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What is your mapping for the UnitData class? Have you mapped an <id> property and how? From the table data you I can't see any column that seems like a primary key.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 4:37 pm 
Newbie

Joined: Wed Nov 12, 2008 12:16 pm
Posts: 15
I have mapped my <id> propery - below is the mapping.

<hibernate-mapping>
<class name="UnitData" table="ONESITE_UNIT">
<id column="ROW_1" name="row1" />

<property column="ROW_2" name="row2" />
<property column="ROW_3" name="row3" />
<property column="ROW_4" name="row4" />
</class>
</hibernate-mapping>

_________________
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 4:54 pm 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
use distinct:
Code:
select distinct data from ...


Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 5:08 pm 
Newbie

Joined: Wed Nov 12, 2008 12:16 pm
Posts: 15
I tried using distinct in my HQL string, but that did not correct my problem either.

_________________
Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 5:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The problem is that you have mapped ROW_1 as <id> when in fact there is more than one row that has the same value. The column used for the <id> attribute should be the primary key of the table or at least have a unique value for each row. If there is no such column in the table you may have to use a composite id that consists of as many column that you need to make up a unique value. Read more about this here: http://www.hibernate.org/hib_docs/v3/re ... ompositeid

This doesn't really answer your original question (why you get 4 objects with the same data), so I'll try to do that as well. Hibernate uses the value of the <id> property to store all loaded objects in an internal cache (the first-level cache). So, the first row is read, a UnitData object is created, stored in the cache, and then returned as a query result. When the second row is about to be read Hibernate first reads the ROW_1 value. Hibernate checks the cache and finds that there is already a UnitData object for that value. Since the cache can only contain a single object for each <id> value Hibernate simply returns the object that is stored in the cache and never reads the remaining columns from the database. The same happens for the remaining rows.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 12, 2008 5:30 pm 
Newbie

Joined: Wed Nov 12, 2008 12:16 pm
Posts: 15
Thanks for your help!! That solved my problem.

_________________
Thanks


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