-->
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.  [ 7 posts ] 
Author Message
 Post subject: Sugestion: Give an option for returning a Map instead a List
PostPosted: Wed Feb 22, 2006 12:07 pm 
Newbie

Joined: Wed Feb 22, 2006 11:56 am
Posts: 4
Location: Campinas, Sãop Paulo, Brazil
Hello there,
I'm currently using Hibernate in a project here.
In a given moment, i have to use a query for searching. The Query returns a List. But I have to get this List and put it into a map strucure...

Like this:

Code:
List peoples = dao.findByAge(age);  //DAO using Hibernate
Map peoplesMap = new HashMap();
for (Iterator it =  peoples.iterator(); it.hasNext; ) {
    People people = (People) it.next();
    peoplesMap.put(people.getName(), people);
}


So the list will be "iterated" twice: once when Hibernate creates it with the results of the query and when my code gets this list and puts its data do a Map.
This may overhead the system. Using pure JDBC, i would create the map while iterating the ResultSet.

Maybe Hibernate next version should have e feature that permits return a map rather than a List, letting me choice which property will be the key and putting the intire object as the value.

Or maybe hibernate do this but I didn't found in the docs :)

_________________
Simplify your J2EE development with Mentawai - XML-Free framework.

Because we want save Java from RoR :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 3:54 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Query.iterate() is your friend :-) You get an Iterator without the (light) overhead of building/fetching a list.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 22, 2006 9:36 pm 
Newbie

Joined: Wed Feb 22, 2006 11:56 am
Posts: 4
Location: Campinas, Sãop Paulo, Brazil
No, I still have to iterate the List.

_________________
Simplify your J2EE development with Mentawai - XML-Free framework.

Because we want save Java from RoR :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 4:29 am 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
I didn't say Query.list().iterator(), I said Query.iterate(). You get an iterator which isn't backed by a List.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 8:59 am 
Beginner
Beginner

Joined: Thu Feb 16, 2006 9:53 am
Posts: 24
I'm facing the same problem but it seems that the "iterate() solution" is not really perfect :

http://www.hibernate.org/hib_docs/v3/re ... ng-iterate

Quote:
This will only usually be the case if you expect that the actual entity instances returned by the query will already be in the session or second-level cache.
If they are not already cached, iterate() will be slower than list() and might require many database hits for a simple query, usually 1 for the initial select which only returns identifiers, and n additional selects to initialize the actual instances.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 23, 2006 11:03 am 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
Thanks for this info, I didn't realize iterate() worked that way. Anyway, I don't think building a list and then fectching it is a big performance it, execpt of course if it has thousands of records.

By the way : putting People objects in a map with 'name' as a key doesn't sound like a good idea to me. Are you sure two people can't have the same name ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 24, 2006 11:28 am 
Newbie

Joined: Wed Feb 22, 2006 11:56 am
Posts: 4
Location: Campinas, Sãop Paulo, Brazil
That's only an example... I cannot paste a real source of my application :(
The key putted in the map is a non-pk, but unique collumn.

_________________
Simplify your J2EE development with Mentawai - XML-Free framework.

Because we want save Java from RoR :)


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