9

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?

Haikal Nashuha
  • 2,958
  • 7
  • 44
  • 67
  • 3
    You want Hibernate to return `ResultSet`s instead of lists of entities? If so, why do you need Hibernate at all? – axtavt Mar 14 '11 at 09:20
  • 2
    Show at least one example why `ResultSet` is better than `List`. I really can't imagine of any one. – BalusC Mar 14 '11 at 11:34
  • 3
    BalusC , I never say that ResultSet is better than List. I know hibernate is there for a reason. I just cant seem to parse the list according to the column name , for example in ResultSet I can do resultSet.getString(index) or resultSet.getString(columnName). In List, apparently I cant do so. Is there anything that I could do for that? – Haikal Nashuha Mar 14 '11 at 23:23
  • 1
    Although this thread is late, but this is the answer for others searching for this question: http://stackoverflow.com/questions/2605385/using-sql-column-names-in-hibernate-createsqlquery-result – craftsman Feb 29 '12 at 19:08
  • Better late than never! That is useful info @craftsman – Haikal Nashuha Mar 01 '12 at 03:43
  • @axtavt Why do I need Hibernate at all? I need it to connect to the database for me. After that, i don't want an ORM. Hence why this question is still relevant 12 years later. – Ian Boyd Jul 15 '22 at 23:48

3 Answers3

4

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();
Vovka
  • 599
  • 3
  • 10
4

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
    }
DABrake
  • 69
  • 6
2

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.

Haikal Nashuha
  • 2,958
  • 7
  • 44
  • 67
  • 1
    Oh, was *that* your actual problem? In the future, try to ask questions with more care. E.g. "How to iterate over a `List` of objects in Java?" instead of asking how to achieve a workaround and/or a different/wrong solution. There are by the way better/cleaner ways to do this, but I'll leave it up to you as exercise :) – BalusC Mar 15 '11 at 02:08
  • uh..there is? Could you care letting me know? I am sorry if my question is not really understandable by the community, not a native english speaker. – Haikal Nashuha Mar 15 '11 at 06:00