-->
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: Saving a set and retrieving first element only
PostPosted: Tue Jan 22, 2008 12:01 pm 
Newbie

Joined: Sat Jan 19, 2008 3:48 am
Posts: 4
Hi All,

Has anyone experienced this? I have a class AppInstance that contains a private variable of type "Set".

private java.util.Set objectSet;

The problem is, when I save a set of multiple objects into a class instance, and then retrieve that set, I retrieve ONLY the first element of that set. For example, set size is only 1.

appInstance.getObjectSet().getSize() is equal to 1 only!

The set is supposed to contain objects of another class that I defined, let's say app2instance objects. Here is my mapping in the hibernate mapping file (app2contentSet is my objectSet variable):

<set
inverse="true"
lazy="false"
name="app2contentSet"
cascade="all"
sort="com.tivella.xtas.application.data.hibernate.application.AppToContentComparator"
>
<key column="app_instance_id" />
<one-to-many class="App2content" />
</set>


The funny thing is that the database saves ALL values that is stored into the Set variable. There seems to be a problem with the retrieval. I assume saving works because the database has the correct data. Does anyone have any suggestions? Please let me know if you need more details. Thanks for listening.

- Joseph
UCLA


Last edited by JUCLAY on Tue Jan 22, 2008 3:07 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Saving a set and retrieving first element only
PostPosted: Tue Jan 22, 2008 12:18 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Do you see the values in DB? Do you have an equal method on the App2Content class? how is the comparator key? Turn on the sql output and see what happens after retrieval point.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 3:20 pm 
Newbie

Joined: Sat Jan 19, 2008 3:48 am
Posts: 4
Thanks for the reply.

1) The values in the database are indeed correct. The Set commits all values to the database, not just one.

2) There is an equals class in my app2content:

public boolean equals (Object obj) {
if (null == obj) return false;
if (!(obj instanceof com.tivella.xtas.application.data.hibernate.application.App2content)) return false;
else {
com.tivella.xtas.application.data.hibernate.application.App2content app2content = (com.tivella.xtas.application.data.hibernate.application.App2content) obj;
if (null == this.getId() || null == app2content.getId()) return false;
else return (this.getId().equals(app2content.getId()));
}
}


3) I am not sure what a comparator key is but here is the comparator class if it helps:

public class AppToContentComparator implements Comparator {

protected final static Logger logger = Logger.getLogger( AppToContentComparator.class );
protected boolean isFirstTime = true;
protected Comparator actualComparator = null;

/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
public int compare(Object o1, Object o2) {

if ( isFirstTime ) {
String cn = o1.getClass().getName() + "Comparator";
try {
Class cl = Class.forName( cn );
actualComparator = (Comparator)cl.newInstance();
}
catch ( Exception e ) {
logger.error( e.getMessage() );
}
}
int res = 0;
if ( actualComparator != null )
res = actualComparator.compare( o1, o2 );

isFirstTime = false;

return res;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 3:31 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Try implementing hashCode() as as well as equals().
Just return the hashCode of your id or 1 if the id is null.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 7:22 pm 
Newbie

Joined: Sat Jan 19, 2008 3:48 am
Posts: 4
hm strange. hashCode() and equals() are already implemented.

public int hashCode () {
if (Integer.MIN_VALUE == this.hashCode) {
if (null == this.getId()) return super.hashCode();
else {
String hashStr = this.getClass().getName() + ":" + this.getId().hashCode();
this.hashCode = hashStr.hashCode();
}
}
return this.hashCode;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 22, 2008 8:10 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
notice that hashCode and equals do not use the same criteria for establishing equality (hashCode is involving the name also). That might be your problem, which would be a Java problem.
The relationship between equals() and hashCode() is as follows:

Quote:
if hashCode is different, the equals has to be different.


The opposite doesn't need to be true, though. Try leaving equals() as it is and returning always 1 from hashcode, and check what happens.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 4:29 am 
Newbie

Joined: Tue Jan 15, 2008 10:07 am
Posts: 14
I'm assuming the getId() method returns the database identifier?
If so, this is really not a good candidate for implementing equals() and hashCode().
Read this for more information.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 12:44 pm 
Newbie

Joined: Sat Jan 19, 2008 3:48 am
Posts: 4
Hm...Hashcode() returning 1 does not seem to change behavior.

In the database table, there is an Id column that is the primary key for each row. If that is what you mean by the database identifier, then yes. Though, I think Hibernate should abstract that, no? Well in any case, I'm reading the link you sent and trying it out. Thanks for the info.


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.