-->
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.  [ 8 posts ] 
Author Message
 Post subject: Query.find() returns List with same objects
PostPosted: Tue Nov 16, 2004 2:59 pm 
Newbie

Joined: Tue Nov 16, 2004 12:11 pm
Posts: 5
[b]Hibernate version: 2.1 [/b]

[b]Mapping documents: [/b]

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs: No Exceptions [/b]

[b]Name and version of the database you are using:[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 3:02 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Why not, duplicates are allowed in a List. Copy it to a HashSet if you don't like it.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 3:24 pm 
Newbie

Joined: Tue Nov 16, 2004 12:11 pm
Posts: 5
Sorry about two posts about the same subject. The one you replied did not have all the necessary information. That was a premature "enter key" action.


I understand lists allow duplicates. But the database tables have two unique customers with id 1 and 2.

But the query.find always returns two customers with same id, 2.


thanks



Here is my orinal post.
-----------------------------------------------------------------------------------


[b]Hibernate version: 2.1 [/b]

[b]Mapping documents: [/b]

[b]Code between sessionFactory.openSession() and session.close():

StringBuffer sb = new StringBuffer(200);
sb.append("select cu ");
sb.append("from Customer as cu, CoCustomer as cc ");
sb.append("where cc.coCustomerId = cu.id ");
sb.append(" and cc.customerId = :customerId");

Query query = session.createQuery(sb.toString());
query.setLong("customerId", customerId);

List list = query.list();

[/b]

[b]Full stack trace of any exception that occurs: No Exceptions [/b]

[b]Name and version of the database you are using: MySql - Version 4.1[/b]

[b]The generated SQL (show_sql=true):


select cocustomer0_.id as id__, cocustomer0_.customerId as customerId__, cocustomer0_.id as id0_, cocustomer0_.customerid as customerid0_, cocustomer0_.cocustomerid as cocustom3_0_, cocustomer0_.status as status0_, cocustomer0_.createdon as createdon0_, cocustomer0_.createdby as createdby0_, cocustomer0_.modifiedon as modifiedon0_, cocustomer0_.modifiedby as modifiedby0_ from tbl_cocustomer cocustomer0_ where cocustomer0_.customerId=? order by cocustomer0_.customerId asc


[/b]

[b]Debug level Hibernate log excerpt:[/b]


Above code returns list of customers, but has same customer objects. For example, the query supposed to return two custoemrs with Id 1,2 but always returns two customers with same Id 2.

If run the SQL statement (generated from show_sql=true logs), i am getting two customers with different Ids. So the sql works as expected.

Is there anything wrong with my Customer.equals and hashCode methods or any other pointer to get pass this issue will be appreciated.

We tried without overridding "equals and hashcode" method and also with orverridding, both ways the results are the same.

thanks


-------------------------------------------------------------------------------


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 3:26 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Check the Hibernate DEBUG-level log.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 16, 2004 9:03 pm 
Newbie

Joined: Tue Nov 16, 2004 12:11 pm
Posts: 5
Here is part of the DEBUG log from hibernate...

Nov 16, 2004 4:49:18 PM net.sf.hibernate.loader.Loader getRow
FINE: result row: 34
Nov 16, 2004 4:49:18 PM net.sf.hibernate.loader.Loader getRow
FINE: result row: 34
Nov 16, 2004 4:49:18 PM net.sf.hibernate.impl.SessionImpl initializeEntity
FINE: resolving associations for [com.nanda.bo.Customer#34]
Nov 16, 2004 4:49:18 PM net.sf.hibernate.impl.SessionImpl initializeEntity
FINE: done materializing entity [com.nanda.bo.Customer#34]
Nov 16, 2004 4:49:18 PM net.sf.hibernate.impl.SessionImpl initializeNonLazyCollections
FINE: initializing non-lazy collections


Looks like it is getting two customer objects with id 34, instead of 33 and 34.

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 3:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
hummm, seems you did something wrong with your id, are you sure you don't have a composite id?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 18, 2004 4:17 am 
Newbie

Joined: Tue Nov 16, 2004 12:11 pm
Posts: 5
No. I dont have a composite id. It is a regular single field primary key.

Here is the hbm file

---------------------------

<hibernate-mapping>

<class name="com.nandha.bo.Customer" table="tbl_customer">

<id column="id" name="id" type="long" unsaved-value="0">
<generator class="native"/>
</id>

<property column="firstname" length="100" name="firstName" not-null="true" type="string"/>
<property column="lastname" length="100" name="lastName" not-null="true" type="string"/>
<property column="middleinitial" length="100" name="middleInitial" type="string"/>
<property column="salutation" length="100" name="salutation" type="string"/>
<property column="address1" length="100" name="address1" not-null="true" type="string"/>
<property column="address2" length="100" name="address2" type="string"/>
<property column="city" length="100" name="city" not-null="true" type="string"/>
<property column="state" length="100" name="state" not-null="true" type="string"/>
<property column="zipcode" length="100" name="zipCode" not-null="true" type="string"/>


<set
name="coCustomers"
lazy="false"
inverse="false"
cascade="all"
order-by="customerId asc" >

<key column="customerId" />
<one-to-many
class="com.nandha.bo.CoCustomer" />
</set>

</class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 4:30 am 
Newbie

Joined: Tue Nov 16, 2004 12:11 pm
Posts: 5
Found out the cause of this problem.

It is my MySql server and JDBC version, that was causing it. I was using MySql version 4.1 Alpha release candidate and JDBC drivers 3.0.6 which was pretty old.

Once I installed MySql 4.1.7 stable release and JDBC 3.1.4 beta things started working fine.

thanks everyone.


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