Awkward spring repositories
-
Upload
steven-ndaye -
Category
Technology
-
view
197 -
download
0
Transcript of Awkward spring repositories
AWKWARD SPRING REPOSITORIES
by Steven Ndaye
JPA Specifications, QueryDSL and StreamAPI to the rescue
Agenda
The repository layer!
An example!
The problem !
The solutions
Repository layer
From Martin Fowler’s blog!
“The repository layer encapsulates the set of objects persisted in a data store
and the operations performed over them, providing a more object-oriented
view of the persistence layer”
Repository layer
In short, it isolates domain objects from details of the database access code.
Java Persistence API
When dealing with entity mappings to the database in the repository layer,
ORMs are used to achieve that. JPA is an ORM specification that provides a POJO persistence model for object
relational mapping.
An example with Spring Data Repositories
Built with the powerful Spring boot
Just by extending CrudRepository we get all the stuff like save, findAll, findOne out of the box. Anything
complex will be interpreted by Spring Data in runtime based on name
conventions and traversals.
The problemLet’s look at the examples
Method names are too long and excessively verbose!
Not easily readable!
Not easy to build new complex queries as we need to rely on Spring Data ability to understand name!
Some methods are not even english valid sentences!
Duplication of criteria among the query method. Less DRY
Would it not be great if we could extract those filters in smaller reusable pieces?
The solutionJPA Specifications
JPA Specification would allow to define a reusable predicate over an entity.
When the predicate is true, the result of the criteria(or query) is returned.!
Let’s look at the example
The solutionQueryDSL
To leverage QueryDSL extension, there is a very specific framework which
enables the construction of statically typed SQL-like queries via its fluent API:
Querydsl!
!
let’s look at the example using QueryDSL in combination with JPA.
QueryDSL gives you all sort of good stuffs: select, from, innerJoin, join,
leftJoin, rightJoin, on, where, groupBy, having, orderBy…!
The solutionStream API
We can send aggregate operations to a sequence of elements from a source.!
In a nutshell we can pass a predicate to an operation.!
!
Let’s look at an example!
Thank youhttps://github.com/StevenNdaye/i-code-java-conf-2016!
@StevenNdaye