Getting the result of a Hibernate query as a result set instead of a list

Hey, I'm new to hibernation. I have to say that 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 a list. Most of the time I really want the result to be in the result set, so that I can more easily manipulate it using the result set, you can specify the value by column name or index. In Liszt, I pretty much own the property.

In some cases, I can get the list into a JSF data table, and then just call the member directly. I can’t always do this. Do not ask me why. @. @ rotating head.

Is there a way to get a result set instead of a sleep list?

+4
source share
3 answers

Well, somehow I managed to get it to work! I'm so happy! For those trying to find how to manipulate the list returned by a Hibernate request, I basically did this.

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

From there, the list should contain the displayed class, and you can access it with an iterator and subsequently use 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.

+2
source

A slightly old thread, but I can not resist:

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

You may need this if you have a huge database and you cannot put the result into memory. Use the scroll () function instead of the 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(); 
0
source

Source: https://habr.com/ru/post/1343606/


All Articles