Hibernate: how to get my objects from ScrollableResults?

I am making a query that returns a list of entities. How to get objects with ScrollableResults:

 Session s  = ....;
 Query q = s.createQuery("....") # returns 100000s rows
 ScrollableResults sr = q.scroll();
 sr.scroll(45999); # just a number
 Employee employee = ???

How do I get an employee in the last line of code

+5
source share
4 answers

try the method get(0)orget()[0]

+6
source

Here's the API reference: ScrollableResults

get()returns the entire current line, get(index)returns the object in position indexwithout initializing the rest. There are also many handy getXXX () methods that cast the result to the given type.

+2
source

, ScrollableResults , Javadocs.

@Bozho, sr.get() , . ScrollableResultsImpl :

    if ( result != null && result.getClass().isArray() ) {
        currentRow = (Object[]) result;
    } else {
        currentRow = new Object[] { result };
    }

ScrollableResults.get() , , get()[0].

, - :

while (sr.next()) {
   // get the entity which is the first element in an Object[]
   Employee employee = sr.get()[0];
   ...
}
0

, , : :

ScrollableResults sr = q.scroll();
while (sr.next()) {
    CustomObject object = (CustomObject) sr.get()[0]; // Now CustomObject will have all the properties mapped
}

.

0

All Articles