How to call stream() on the Hibernate CriteriaImpl object


#1

Hello,

I have a class that extends org.hibernate.internal.CriteriaImpl but as far as I can see the API of that class does not provide an stream() method, like the one provided by org.hibernate.query.Query class.

Why is this class not providing such method? Is there any chance of using streams with criteria?

Thank you in advance.


#2

CriteriaImpl is an internal class so you shouldn’t extend it.

Considering the criteria API is deprecated in favor of the JPA criteria, I don’t see us making changes in it.

Note that I wouldn’t recommend to use the JPA criteria API directly as it’s barely usable but things like QueryDSL helps a lot.


#3

If you use Criteria API, you can use the stream method either via JPA 2.2 getStreamResult() or if you unwrap the JPA Query to an org.hibernate.query.Query and call stream.

However, just because you can, doesn’t mean you should. Most often, pagination is a much better alternative to streaming.


#4

Hello,

ok, understood. Thank you very much @gsmet and @vlad for the information.