开发者

Retrieving Hibernate query result as result set instead of list

Heya, I m new to hibernate. I have to say it really simplifies everything for the SQL query. However, manipulating the returned result is a headache for me at the moment.

The result is returned as list. Most of the time I really want the result to be in result set so that I can manipulate it easier as using result set, you can specifies value by column name or index. In List, I am pretty much owned by own noobity.

In some ocassion, I can retrieve the list into a JSF data table and then just call the member directly. I cannot always do this. Don't ask me why. @.@ spinning head.

Is there a way to get resultset instead开发者_开发百科 of list for hibernate?


Slightly old thread but I cannot resist:

    // Code for iterating over a list of objects
    for(MappedClass mappedCless : list){
        String name = mappedClass.getName();
        String id = mappedClass.getId();
        // further logic
    }


You may need this if you have huge database and you can't fit List result into memory. Use scroll() instead of list():

Query query = session.createQuery(query);
query.setReadOnly(true);
setFetchSize(Integer.MIN_VALUE); //MUST use Integer.MIN_VALUE, other value=fetch all
ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
// iterate over results
while (results.next()) {
    Object row = results.get();
}
results.close();


Ok, somehow I managed to make it work! Me so happy! For those who trying to find on how to manipulate the list returned by Hibernate query, basically what I did was..

//previous code
list = (List<MappedClass>)query.list();

From there the list shall contain mapped class and you can access it by iterator and subsequently uses getter to retrieve the value. Example

//previous code
for (int i =0; i<list.size(); i ++) {
  String name;
  String id;
  name = list.get(i).getName();
  id = list.get(i).getId();

  //add your data manipulation here
}

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜