-->
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.  [ 14 posts ] 
Author Message
 Post subject: createQuery().list()-- the resultset has the same element
PostPosted: Fri Oct 14, 2005 1:57 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
Hibernate version: 2.0

[b]My Sql Query is :


String SQL_QUERY ="from MaOwnXref as A where A.OWN_MA_USR_ID= 'MSTRADM'" ;

ArrayList lst = (ArrayList) hSession.createQuery(SQL_QUERY) .list();


The database is MS Sql.

Now when i view the contents of the list "lst" in debug mode i see many rows in the list but all the elements in the list are the same.

When i execute the same Sql query that appeared on the console in Query Analyzer -- i get many rows as resultset.

Can anyone help me?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 2:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you most likely have more than one row with the same id...not healthy ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 7:57 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
Which id you are speaking of?

My database table has the following collumns
OWN_MA_USR_ID
MA_USR_ID

This table represents the parent-child relationship. where OWN_MA_USR_ID is the parent and MA_USR_ID is the child. So one parent can have many childs. so i am trying to retreive all the childrens of a particular parent.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 8:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
btw. you do know that the query you show is a HQL query not SQL query ?

i dont know how you have mapped your object model so can't help you much.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 8:57 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
i got what the problem is but i dont know how to solve it --


The problem is that the collumns of the table --OWN_MA_USR_ID and MA_USR_ID together form a composite key and both are strings --

and i changed the mapping file as

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

<?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 package="com.abcea.mas.tablemapping">
<class name="com.abcea.mas.tableobj.MaOwnXref" table="MA_OWN_XREF_T">
<composite-id unsaved-value="none">
<key-property name="OWN_MA_USR_ID" type="java.lang.String"/>
<key-property name="MA_USR_ID" type="java.lang.String"/>
</composite-id>
</class>
</hibernate-mapping>

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

now its asking me to implement equals() and hashcode() in the persistent class.
i have implemeted equals() but not able to implement hashcode() as both the table collumns are strings.

can anyone tell me how to implement hashcode here?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 9:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
use the search abhilash!

http://hibernate.org/109.html

besides a long text about what is important about equals and hashcode it also has links to pages explaining the basics for writing equals and hashcodes

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 9:02 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Why can't you implement HashCode on Strings ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 1:19 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
Thanks -- Max and Preston.

I have implemented equals and hashcode. But the equals method is slowing down my Query.

Can you give me some suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 1:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
what do you do in your equals method since it hogs down your query ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:35 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
my equals method looks like this:


public boolean equals(Object other) {
if (this == other) return true;
if ( !(other instanceof MaOwnXref) ) return false;

final MaOwnXref cat = (MaOwnXref) other;

if ( !cat.getOWN_MA_USR_ID().equalsIgnoreCase( getOWN_MA_USR_ID() ) ) return false;
if ( !cat.getMA_USR_ID().equalsIgnoreCase( getMA_USR_ID() ) ) return false;

return true;
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 2:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
that isn't special and if that affects your query your query must be insanely big ?

why is ignore case relevant ?

and did you remember to implement a proper hashcode impl ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 3:05 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
i have IgnoreCase because OWN_MA_USR_ID and MA_USR_ID are strings.

It is mainly slow because my Query returns a large result set - some 1300 rows. but i works perfectly fine when i have a small resultset.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 3:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
but if you hashcode has a good spread of values then this should not affect you at all with 1300 elements

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 8:12 am 
Newbie

Joined: Fri Oct 14, 2005 1:27 am
Posts: 14
Location: Bangalore
Thanks max,
the problem was with hashcode() method -- i implemented it properly and now there are no problems.


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