StreamSuppliers that can be reused to create Streams of the same Entity source,
see createStreamSupplier(StreamConfiguration)
Entity sources can be RDBMSes, files or other data sources. A JPAStreamer must be thread safe and be able to handle several reading and writing threads at the same time.
- Since:
- 0.1.0
- Author:
- Per Minborg
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this JPAStreamer and releases any resources potentially held.static JPAStreamerBuildercreateJPAStreamerBuilder(jakarta.persistence.EntityManagerFactory entityManagerFactory) Creates and returns a new JPAStreamerBuilder that will use the providedentityManagerFactory.static JPAStreamerBuildercreateJPAStreamerBuilder(String persistenceUnitName) Creates and returns a new JPAStreamerBuilder that will create a newEntityManagerFactoryusing the providedpersistenceUnitName.static JPAStreamerBuildercreateJPAStreamerBuilder(Supplier<jakarta.persistence.EntityManager> entityManagerSupplier) Creates and returns a new JPAStreamerBuilder that will use the providedentityManagerSupplier.default <T> StreamSupplier<T>createStreamSupplier(Projection<T> projection) Creates and returns a newStreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of theentityspecified by the providedprojection.<T> StreamSupplier<T>createStreamSupplier(StreamConfiguration<T> streamConfiguration) Creates and returns a newStreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of the provided typeentityClass.default <T> StreamSupplier<T>createStreamSupplier(Class<T> entityClass) Creates and returns a newStreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of the provided typeentityClass.static JPAStreamerof(jakarta.persistence.EntityManagerFactory entityManagerFactory) Creates and returns a new JPAStreamer that will use the providedentityManagerFactory.static JPAStreamerCreates and returns a new JPAStreamer that will create a newEntityManagerFactoryusing the providedpersistenceUnitName.static JPAStreamerCreates and returns a new JPAStreamer that will use the providedentityManagerSupplier.voidresetStreamer(Class<?>... entityClasses) Deprecated, for removal: This API element is subject to removal in a future version.since 3.0.2, JPAStreamer no longer caches Streamers thus there is no need for a method that resets the cache.default <T> Stream<T>stream(Projection<T> projection) Creates and returns a newStreamover all entities in the underlying data source (e.g database) of theentityspecified by the providedprojection.<T> Stream<T>stream(StreamConfiguration<T> streamConfiguration) Creates and returns a newStreamover all entities in the underlying data source (e.g database) according to the providedstreamConfiguration.default <T> Stream<T>Creates and returns a newStreamover all entities in the underlying data source (e.g database) of the provided typeentityClass.
-
Method Details
-
stream
Creates and returns a newStreamover all entities in the underlying data source (e.g database) according to the providedstreamConfiguration. This is the main query API for JPAstreamer.The order in which elements are returned when the stream is eventually consumed is unspecified. The order may even change from one invocation to another. Thus, it is an error to assume any particular element order even though is might appear, for some stream sources, that there is a de-facto order.
If a deterministic order is required, then make sure to invoke the
Stream.sorted(java.util.Comparator)method on theStreamreturned.Mutable elements are not reused within the stream. More formally, there are no pair of mutable stream elements
e1ande2such thate1 == e2.The Stream will never contain
nullelements.This is an inexpensive O(1) operation that will complete in constant time regardless of the number of entities in the underlying database.
The returned stream is aware of its own pipeline and will optionally optimize its own pipeline whenever it encounters a Terminal Operation so that it will only iterate over a minimum set of matching entities.
When a Terminal Operation is eventually called on the
Stream, that execution time of the Terminal Operation will depend on the optimized pipeline and the entities in the underlying database.The Stream will be automatically
closedafter the Terminal Operation is completed or if an Exception is thrown during the Terminal Operation.Some of the Terminal Operations are:
forEach(Consumer)forEachOrdered(Consumer)toArray()toArray(IntFunction)reduce(BinaryOperationreduce(Object, BinaryOperator)reduce(Object, BiFunction, BinaryOperator)collect(Collector)collect(Supplier, BiConsumer, BiConsumer)min(Comparator)min(Comparator)count()anyMatch(Predicate)noneMatch(Predicate)findFirst()findAny()iterator()
Any Terminating Operation may throw an Exception if the underlying database throws an Exception (e.g. an SqlException)
Because the Stream may short-circuit operations in the Stream pipeline, methods having side-effects (like
peek(Consumer)will potentially be affected by the optimization.Here are some examples of how the stream optimization might work:
-
stream(Film.class) .filter(Film$.name.equal("Casablanca")) .collect(toList());-> select * from film where name='Casablanca' -
stream.count();-> select count(*) from film -
stream(Film.class) .filter(Film$.name.startsWith("A")) .count();-> select count(*) from hares where name LIKE 'A%' -
stream.stream(Film.class) .filter(Film$.rating.equal("G") .filter(Film$.length.greaterThan(100) .count();-> select count(*) from hares where rating ='G' and length > 100
- Type Parameters:
T- The element type (type of a class token)- Parameters:
streamConfiguration- a configuration including an entity class (annotated with@Entity)- Returns:
- a new
Streamover all entities in the underlying data source (e.g database) described by the providedstreamConfiguration - Throws:
RuntimeException- if an error occurs during a Terminal Operation (e.g. an SqlException is thrown by the underlying database)- See Also:
-
stream
Creates and returns a newStreamover all entities in the underlying data source (e.g database) of the provided typeentityClass.This method is a convenience method equivalent to:
stream(StreamConfiguration.of(entityClass))- Type Parameters:
T- The element type (type of a class token)- Parameters:
entityClass- to use- Returns:
- a new
Streamover all entities in the underlying data source (e.g database) of the provided typeentityClass - See Also:
-
stream
Creates and returns a newStreamover all entities in the underlying data source (e.g database) of theentityspecified by the providedprojection.This method is a convenience method equivalent to:
stream(StreamConfiguration.of(projection.entityClass()).select(projection))- Type Parameters:
T- The element type (type of a class token)- Parameters:
projection- to use- Returns:
- a new
Streamover all entities in the underlying data source (e.g database) of theentityspecified by the providedprojection. - See Also:
-
createStreamSupplier
Creates and returns a newStreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of the provided typeentityClass.The provided
StreamSupplierwill not be closed whenever the generatedStreaminstance is closed.If you are using the same Stream source frequently e.g. Film.class, consider configuring a
StreamSupplierthat can supplyStreams from the same source over and over again. This save resources and avoids instantiating a newEntityManagerfor each newStream.Here is an example of using a
StreamSupplier:
The above is equal to:final StreamSupplier<Film> streamSupplier = jpaStreamer.createStreamSupplier(Film.class); List<Film> longFilms = streamSupplier.stream() .filter(Film$.name.equal("Casablanca")) .collect(toList()); // the terminal operation does not close the Stream Supplier and its Entity Manager // ... repeated uses of the supplier streamSupplier.close(); // closes the Entity ManagerList<Film> films = jpaStreamer.stream(Film.class) .filter(Film$.name.equal("Casablanca")) .collect(toList()); // the terminal operation closes the underlying StreamSupplier and its Entity Manager- Type Parameters:
T- The element type (type of a class token)- Parameters:
streamConfiguration- a configuration including an entity class (annotated with@Entity)- Returns:
- a new
StreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) described by the providedstreamConfiguration
-
createStreamSupplier
Creates and returns a newStreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of the provided typeentityClass.This method is a convenience method equivalent to:
createStreamer(StreamConfiguration.of(entityClass))- Type Parameters:
T- The element type (type of a class token)- Parameters:
entityClass- to use in generatedStreams- Returns:
- a new
StreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of the provided typeentityClass - See Also:
-
createStreamSupplier
Creates and returns a newStreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of theentityspecified by the providedprojection.This method is a convenience method equivalent to:
createStreamer(StreamConfiguration.of(entityClass))- Type Parameters:
T- The element type (type of a class token)- Parameters:
projection- to use- Returns:
- a new
StreamSupplierthat can createStreams over all entities in the underlying data source (e.g database) of theentityspecified by the providedprojection. - See Also:
-
resetStreamer
Deprecated, for removal: This API element is subject to removal in a future version.since 3.0.2, JPAStreamer no longer caches Streamers thus there is no need for a method that resets the cache. If you wish to manage the Streamer lifecycle manually, seecreateStreamSupplier(StreamConfiguration)Resets the Streamer associated with the provided Entity classes.This will create a new instance of the underlying
jakarta.persistence.EntityManager, removing all entries of the associated Entity class from the first-level cache. The oldjakarta.persistence.EntityManageris closed upon removal from the cache. In case JPAStreamer was configured with aSupplier<EntityManager>the lifecycle of the Entity Managers is not managed by JPAStreamer, thus use of the method is not permitted and will result in anUnsupportedOperationException.- Parameters:
entityClasses- of the streamer- Throws:
UnsupportedOperationException- if JPAStreamer is configured with a Supplier, seecom.speedment.jpastreamer.application.JPAStreamer#of(java.util.function.Supplier)
-
close
void close()Closes this JPAStreamer and releases any resources potentially held.If and only if this JPAStreamer was created using a
persistenceUnitName, the underlying EntityManagerFactory will be closed. -
createJPAStreamerBuilder
Creates and returns a new JPAStreamerBuilder that will create a newEntityManagerFactoryusing the providedpersistenceUnitName.Call the JPAStreamerBuilder::build method to create a new JPAStreamer instance.
The newly created EntityManagerFactory will be closed whenever a built JPAStreamer instance is closed.
- Parameters:
persistenceUnitName- of the persistence unit as per the persistence.xml file- Returns:
- a new JPAStreamerBuilder
-
createJPAStreamerBuilder
static JPAStreamerBuilder createJPAStreamerBuilder(jakarta.persistence.EntityManagerFactory entityManagerFactory) Creates and returns a new JPAStreamerBuilder that will use the providedentityManagerFactory.Call the JPAStreamerBuilder::build method to create a new JPAStreamer instance.
The provided
entityManagerFactorywill not be closed whenever a built JPAStreamer instance is closed.- Parameters:
entityManagerFactory- to be used by the JPAStreamer- Returns:
- a new JPAStreamerBuilder
-
createJPAStreamerBuilder
static JPAStreamerBuilder createJPAStreamerBuilder(Supplier<jakarta.persistence.EntityManager> entityManagerSupplier) Creates and returns a new JPAStreamerBuilder that will use the providedentityManagerSupplier.Call the JPAStreamerBuilder::build method to create a new JPAStreamer instance.
EntityManagers provided by the
entityManagerSupplierwill not be closed whenever a built JPAStreamer instance is closed.This is a preview and may be subject to change.
- Parameters:
entityManagerSupplier- to be used by the JPAStreamer- Returns:
- a new JPAStreamerBuilder
- Since:
- 1.1.1
-
of
Creates and returns a new JPAStreamer that will create a newEntityManagerFactoryusing the providedpersistenceUnitName.The newly created EntityManagerFactory will be closed whenever the returned JPAStreamer instance is closed.
- Parameters:
persistenceUnitName- of the persistence unit as per the persistence.xml file- Returns:
- a new JPAStreamer
- See Also:
-
of
Creates and returns a new JPAStreamer that will use the providedentityManagerFactory.The provided
entityManagerFactorywill not be closed whenever the returned JPAStreamer instance is closed.- Parameters:
entityManagerFactory- to be used by the JPAStreamer- Returns:
- a new JPAStreamerBuilder
-
of
Creates and returns a new JPAStreamer that will use the providedentityManagerSupplier.EntityManagers provided by the
entityManagerSupplierwill not be closed whenever the returned JPAStreamer instance is closed.- Parameters:
entityManagerSupplier- to be used by the JPAStreamer- Returns:
- a new JPAStreamerBuilder
-