-->
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.  [ 6 posts ] 
Author Message
 Post subject: need help about Sorted collections
PostPosted: Wed May 31, 2006 11:04 pm 
Newbie

Joined: Tue Apr 25, 2006 12:21 am
Posts: 5
Location: Sri lanka
I am new to hibernate.

I am using JDK1.5, jboss-4.0.3SP1, and Eclipse SDK.
I need to get a collection sorted. I have used order-by attribute to sort the collection

<set name="users" table="Application_Users" order-by="userid asc">

Set users = application.getUsers();

This code works fine and gives me an orderd set when I run it in a JUnit testcase. But when it is deployed in jboss and accessed via a jsp page, the order of elements in the set varies from time to time.

Hibernate reference document says;
" order-by (optional, JDK1.4 only) specify a table column (or columns) that define the iteration order of the Map, Set or bag, together with an optional asc or desc ". Cant I use order-by with JDK1.5

How else can I get an orderd set?

Please help me.
Thanks in advance

_________________
Best regards,
Nadikani


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 12:21 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The comment implies 1.4 and above. It does work in java5.

I've never noticed the deterministic order vary when using order-by. Non-deterministic ordering does change, of course. So if you have multiple members in the set with the same userid, the order of those members is not well defined.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 12:50 am 
Newbie

Joined: Tue Apr 25, 2006 12:21 am
Posts: 5
Location: Sri lanka
Thanks for the comment.
No there are no multiple members in the set with same userid.

Application and Users objects have bydirectional many-to-many relationship whihch I have implemented with sets in both entities. The junction table:
Application_Users has following data

Application_ID userid
1 1
1 2
1 3
1 4

In my code I load Application object and then get the set of users.
(Set users = application.getUser();)

I have a testcase to print the usernames insde a for loop for the above set.
running the test in junit test case gives following results

test run 1 test run 2
Name1 Name1
Name2 Name2
Name3 Name3
Name4 Name4

This result is consistant and its well orderd in all .

But if i run this same test code in a jsp deployed in jboss the result

test run 1 test run 2 test run 3
Name2 Name3 Name4
Name1 Name2 Name2
Name3 Name1 Name3
Name2 Name2 Name2

I have no idea what couses this.

Please help me to resolve this.

Thanks.

_________________
Best regards,
Nadikani


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 1:02 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Unless you've got a few typos there, that's obviously not a problem with order-by. There are repeated entries and omitted entries, as well as badly-sorted entries, in what you've posted (test run 1: name2 name1 name3 name2. No name4 at all). Obviously there's something more fundamental wrong here.

First, double-check that the mapping files you're using in the deploy instance are the same as the ones you're running the junit test with. Sometimes, it's the little things...

Next check for synchronization issues. Synchronize your test code on some global value (e.g. your session factory: don't use a session object, as that's different in different threads). If that resolves it, then your container is reusing objects that it shouldn't be (perhaps sharing sessions across threads outside of a CMT environment?).

Finally, debug the test.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 1:44 am 
Newbie

Joined: Tue Apr 25, 2006 12:21 am
Posts: 5
Location: Sri lanka
Thanks Tenwit, and sorry for the inconvinience coused from mistakes in test results.

Those mistakes are due to the manual editing to make it clear for displaying purposes. Actually There are no repeated entries or omitted entries.

And also the mapping files and other files are identical in both tests.


test run 1 test run 2 test run 3
Name2 Name3 Name4
Name1 Name4 Name1
Name3 Name1 Name3
Name4 Name2 Name2

Code:
public Application readApplication(Long id){
  Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  session.beginTransaction();
  Application app = (Application) session.load(Application.class, id);
  session.getTransaction().commit();
  return app;
}
      



Code:
Application app =  businessServises.readApplication(id);
Set users = app.gerUsers();
for(int i=0; i<users.size;i++){
  User u = (User)users.toArray()[i];
  System.out.println(u.getName());
}



Any more help is highly admired.

Thank you

_________________
Best regards,
Nadikani


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 01, 2006 5:35 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Fair enough. Only two more possibilities occur to me:

Your test run output sort of suggests that you're running the tests concurrently. Is that the case? Or just one at a time? If it's concurrect, is it possible that you're seeing the contents of the various sets mixed up due to the printouts interleaving?

Or, if the hibernate code and web/application server are talking to each other via serialized objects or otherwise encoded objects, is it possible that the sets are correctly sorted when hibernate hands them over to your code, but they lose their sorting during transmission to the web server? It's possible that Hibernate's srtoed PersistentSet instance doesn't maintain ordering when serialized. Or even that it is serialized as a Set, and reconstiuted as an unordered HashSet.

I'm afraid I don't know enough about the inner workings of JBoss to figure out if that's what's happening.

_________________
Code tags are your friend. Know them and use them.


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