5

I'm new to JPA and I'd like to know if it is possible to stream data from a result set, I mean I do not want to wait that the query is performed to start dealing with first results, for instance in the case of a batch.

Is there any possibility using the JPA API or any community adopted workaround ? Eventually using a feature of a JPA implementation ?

Kara
  • 6,115
  • 16
  • 50
  • 57
snowflake
  • 1,750
  • 2
  • 17
  • 40
  • In fact of "stream" the term "scroll" seems closer to what I expressed. I found an implementation (Query.scroll) in Hibernate, which allows to do browse the results as they arrive, but I can't find anything related in the JPA specification https://www.hibernate.org/hib_docs/v3/api/org/hibernate/Query.html#scroll%28%29 – snowflake Feb 25 '10 at 15:56

4 Answers4

1

Is using @javax.persistence.PostLoad annotation viable option for you? This way you can hook up action to the moment when given object is created from the data store. I am not sure if this is exactly what you are looking for.

Jarek Rozanski
  • 780
  • 1
  • 6
  • 13
1

For obvious reasons the "master" select must finish before anything can be done on the result set. I'm not sure what you're trying to achieve here... Perhaps you need to make some fields lazy in order to get first result faster and fetch details as you process them?

Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
1

You could use the real streams on JPA:

public interface UserRepository extends JpaRepository<User, Integer> {
    // ...
    Stream<User> findAllByName(String name);
    // ...
}

You can find more examples in this nice article.

tm1701
  • 7,307
  • 17
  • 79
  • 168
0

Nowadays javax.persistence.TypedQuery has now getResultStream() method which streams the data, however the default implementation only wraps getResultList() into a stream (so, it anyway loads all data into memory). But Hibernate overrides this behavior with real streaming.

Lukasz Frankowski
  • 2,955
  • 1
  • 31
  • 32