awkward spring repositories
TRANSCRIPT
![Page 1: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/1.jpg)
AWKWARD SPRING REPOSITORIES
by Steven Ndaye
JPA Specifications, QueryDSL and StreamAPI to the rescue
![Page 2: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/2.jpg)
Agenda
The repository layer!
An example!
The problem !
The solutions
![Page 3: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/3.jpg)
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”
![Page 4: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/4.jpg)
Repository layer
In short, it isolates domain objects from details of the database access code.
![Page 5: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/5.jpg)
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.
![Page 6: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/6.jpg)
An example with Spring Data Repositories
Built with the powerful Spring boot
![Page 7: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/7.jpg)
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.
![Page 8: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/8.jpg)
The problemLet’s look at the examples
![Page 9: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/9.jpg)
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
![Page 10: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/10.jpg)
![Page 11: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/11.jpg)
Would it not be great if we could extract those filters in smaller reusable pieces?
![Page 12: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/12.jpg)
The solutionJPA Specifications
![Page 13: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/13.jpg)
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
![Page 14: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/14.jpg)
The solutionQueryDSL
![Page 15: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/15.jpg)
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.
![Page 16: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/16.jpg)
QueryDSL gives you all sort of good stuffs: select, from, innerJoin, join,
leftJoin, rightJoin, on, where, groupBy, having, orderBy…!
![Page 17: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/17.jpg)
The solutionStream API
![Page 18: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/18.jpg)
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!
![Page 19: Awkward spring repositories](https://reader031.vdocument.in/reader031/viewer/2022030303/587c75bb1a28abd04e8b6a3b/html5/thumbnails/19.jpg)
Thank youhttps://github.com/StevenNdaye/i-code-java-conf-2016!
@StevenNdaye