-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query returns result in wrong order
PostPosted: Thu Nov 20, 2003 10:57 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
Hi,

i use the following code to select a number of BOCustomer objects from a number of primary keys:

Code:
List  ids   = ...
Query query = pSession.createQuery("select from BOCustomer as customer " +
                                   "where customer.id in (:ids)");
query.setParameterList("ids", ids);
List results= query.list();


Unfortunately the order of the BOCustomer objects doesn't match the order of the id List.

Any hints?

Bye,

J


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 11:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No .... why should it?

SQL doesn't behave that way....


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 11:11 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
gavin wrote:
No .... why should it?

SQL doesn't behave that way....

Come on gavin, at least have a hint in the docs for us mere mortals ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 11:32 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Well, Gavin is right, (shock, horror. :P) SQL works in such a way, that it goes through your table of focus, i.e. BOCustomer and for each row in its table, it checks your list. That means, if you want the resulting list, to match your input list, well, order your input list either ascending or descending (use a bubblesort algo. or something) and tell the SQL query to return the list "order by customer.id asc", or desc, depending on what you did with your list.

That's about the only way you'll be able to do it. AFAIK.

-G


Top
 Profile  
 
 Post subject: Re: Query returns result in wrong order
PostPosted: Fri Nov 21, 2003 5:24 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
Just for the records, here is my solution:

Code:
List  ids   = ...
Query query = pSession.createQuery("select from BOCustomer as customer " +
                                   "where customer.id in (:ids)");
query.setParameterList("ids", ids);
List results= query.list();

// sync the order of the result list with the order of the keys list
int  n   = results.size();
List back= new ArrayList(n);
Map  map = new HashMap  (n); // key= id, value= customer
for (int i = 0; i < n; i++)
{
    BOCustomer customer= (BOCustomer) results.get(i);
    Long       id      = new Long(customer.getId());
    map.put(id, customer);
}
for (int i = 0, n = ids.size(); i < n; i++)
{
    Long       id      = (Long)ids.get(i);
    BOCustomer customer= (BOCustomer) map.get(id);
    back.add(customer);
}



Bye,

J


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