开发者

Is it possible using JPA to stream results from javax.persistence.Query.getResultList()?

开发者 https://www.devze.com 2022-12-21 13:43 出处:网络
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 开发者_Python百科to start dealing with first results,

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 开发者_Python百科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 ?


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?


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.


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.


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.

0

精彩评论

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

关注公众号