JPAstreamer allows efficient and intuitive database queries using Java streams by integrating the Java stream API with JPA. It works by generating a metamodel at compile time and using an annotation processor and stream renderer to convert stream queries into optimized JPA queries that can be executed by the underlying JPA provider.
2. Problems
Mixing Hibernate/JPA and Java for Database actions
is neither completely type-sage nor intuitive.
Tedious process to switch between two languages
and HQL and JPQL syntax is wordy.
3. Solution
The Java Stream API is
efficient, terse and type-safe
intuitive to express application logic with java streams
Adding JPAstreamer make
Your database application
efficient, terse and type-safe
intuitive to express application logic with java streams
4. How it fits?
JPAstreamer is added as a lightweight dependency
to the Maven/Gradle build. It integrates with the
JPA layer by rendering the Stream queries to JPA
and passing them to the underlying JPA provider
for execution.
5. How it works?
This enriches the API of an existing JPA provider to include Java
Streams to express queries.
Key parts:
The annotation processor
Generates metamodel used to compose predicates that can be interpreted by the Stream
renderer
The Stream renderer
Inspects the Stream pipelines and renders optimized JPA queries.
6. Under the hood
JPAstreamer uses and annotation processor to form a metamodel at compile
time
It inspects any classes marked with the standard JPA annotation @Entity
For every entity Foo.class, a corresponsing Foo$.class is generated
Classes can take the form Foo$.firstName.startWith() that can ne
interpreted by JPAstreamers query optimizer.
8. The Stream renderer
The StreamRenderer interface in
JPAStreamer is responsible for
converting the result set of a JPA
query into a stream of objects that
can be processed using Java Streams.
11. The problem
Need to map between different models
View models vs DB models
3rd party REST api vs DB model
Types are similar, but not quite the same
12. Problem Solutions
Manually
Boring
Error prone (might miss mapping)
Reflection-based libraries
Lack of type-safe
Performance penalty
Difficult debugging
13. MapStruct
Mappings are defined as interfaces
Mapping java code generated at compile time using annotation processor
No reflection
No runtime dependencies
14. Advantages of code generation
Type-safe
Quick feedback loop
Easy to debug
Plain method calls -> very fast
No runtime dependencies
Works on CLI or in your IDE