-->
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: question about collection sort in memory
PostPosted: Tue May 23, 2006 1:40 am 
Newbie

Joined: Tue May 23, 2006 1:07 am
Posts: 4
-----------------------------------
database table:
Users:
userId not null int primary key
...
SystemMenu:
menuId not null int primary key
...
user_menu:(the map of Users and SystemMenu)
userId int not null primary key
menuId int not null primary key
foreign key (userId) references Users(userId)
foreign key (menuId) references SystemMenu (menuId)
-----------------------------------
The Record Of Users (omit):
userId
1

The Record Of SystemMenu (omit):
menuId
1
2
3
4
5
6

The Record Of user_menu (omit):
userId menuId
1 1
1 2
1 3
1 4
1 5
1 6
-----------------------------------
public class
{
private Integer userId;
private Set systemMenus = new TreeSet();
...
}

public class SystemMenu
{
private Integer menuId;
...
}
-----------------------------------
User.hbm.xml
<hibernate-mapping>
<class name="User" table="Users">
<id name="userId" type="integer" column="userId">
<generator class="native" />
</id>
...
<set name="systemMenus" table="user_menu" lazy="false" inverse="false" order-by="menuId" sort="natural">
<key column="userId" />
<many-to-many column="menuId"class="SystemMenu" />
</set>
</class>
</hibernate-mapping>

SystemMenu.hbm.xml
<hibernate-mapping>
<class name="SystemMenu" table="SystemMenu">
<id name="userId" type="integer" column="userId">
<generator class="native" />
</id>
...
</class>
</hibernate-mapping>
-----------------------------------
requirement:
given a user,then get it's systemMenus,but the systemMenus must be sorted,the order must be according to the primary key of the table.
On the jsp page,I want to output the data by use iterator,it may like this:

SystemMenu parentMenu=null;
Collection data=(Collection)request.getAttribute("data");
Iterator parentIt=(Iterator)data.iterator();
while(parentIt.hasNext())
{
parentMenu=(SystemMenu)parentIt.next();
out.println(parentMenu.getMenuName());
}

But,where I refresh the jsp page,the result of every time is different.
in order to resolve the problem,I have use the TreeSet,also I defined the sort in User.hbm.xml.
Now,there is some Exception.
-----------------------------------
Excepiton:
java.lang.ClassCastException: SystemMenu
at java.util.TreeMap.compare(TreeMap.java:1093)
at java.util.TreeMap.put(TreeMap.java:465)
at java.util.TreeSet.add(TreeSet.java:210)
at java.util.AbstractCollection.addAll(AbstractCollection.java:318)
at java.util.TreeSet.addAll(TreeSet.java:258)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:273)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollection
...
-----------------------------------
If I remove " sort='natural' " from "User.hbm.xml" ,then this is no exception,I want to know why and how to resolve this problem.
Thanks for everyone.


Last edited by cwbnig on Tue May 23, 2006 9:01 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 2:08 pm 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Hi!

You tried pretty hard to obfuscate your problem. If you edit the stack-trace before asking for help, you should try not to hide the actual exception. :(
However, I assume that it is a ClassCastException. Objects that are stored in a TreeSet (see http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html) (or in a SortedSet in general) have to implement the Comparable interface. You should check the Javadoc on the TreeSet (especially the part concerning "consistency with equals").

All the best,

René


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 9:23 pm 
Newbie

Joined: Tue May 23, 2006 1:07 am
Posts: 4
Dear ReneStolp:
Thank you very much.
I am a chinese,my englist is not very good,so there may be some mistake in my post.
I have edit the exception.The exception is ClassCastException.Your assume is correct.
I also edit the java class:SystemMenu

public class SystemMenu implements Comparator
{
private Integer menuId;

public int compare(Object o1, Object o2)
{
SystemMenu sm1 = (SystemMenu) o1;
SystemMenu sm2 = (SystemMenu) o2;
return sm1.getMenuId().intValue() - sm2.getMenuId().intValue();
}

public boolean equals(Object obj)
{
boolean val = false;
SystemMenu sm = (SystemMenu) obj;
if (this.getMenuId().intValue() == sm.getMenuId().intValue())
{
val = true;
}
return val;
}
...
}

But,there is still some error in my program:
java.lang.ClassCastException:SystemMenu


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 11:14 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
cwbnig wrote:
public class SystemMenu implements Comparator

Uops! Implements Comparable instead Comparator.

valeuz...

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 1:30 am 
Newbie

Joined: Tue May 23, 2006 1:07 am
Posts: 4
Dear Marcos Silva Pereira:
Thank you very much.
I have edit my program.
There is no exception.
But the data is not sorted.

public class SystemMenu implements Comparable
{
private Integer menuId;
public int compareTo(Object obj)
{
SystemMenu sm = (SystemMenu) obj;
return this.getMenuId().intValue() - sm.getMenuId().intValue();
}
...
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 4:37 am 
Newbie

Joined: Tue May 23, 2006 1:07 am
Posts: 4
Thank's for every one.
I have resolve my problem.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 4:43 am 
Beginner
Beginner

Joined: Sat Dec 17, 2005 1:24 pm
Posts: 42
Location: Berlin, Germany
Hi!

Quote:
I have resolve my problem.


Glad to hear that. You may want to rate the responses if they helped (see http://forum.hibernate.org/credits.html).

All the best,

René


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 24, 2006 7:44 pm 
Regular
Regular

Joined: Thu Jul 08, 2004 1:21 pm
Posts: 68
Location: Recife - Pernambuco - Brazil
cwbnig wrote:
Thank's for every one.
I have resolve my problem.

Please, share with us how do you solve the problem. ;-)

Kind Regads,

_________________
Marcos Silva Pereira
http://blastemica.blogspot.com


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.