Awkward spring repositories

19
AWKWARD SPRING REPOSITORIES by Steven Ndaye JPA Specifications, QueryDSL and StreamAPI to the rescue

Transcript of Awkward spring repositories

Page 1: Awkward spring repositories

AWKWARD SPRING REPOSITORIES

by Steven Ndaye

JPA Specifications, QueryDSL and StreamAPI to the rescue

Page 2: Awkward spring repositories

Agenda

The repository layer!

An example!

The problem !

The solutions

Page 3: Awkward spring repositories

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

Repository layer

In short, it isolates domain objects from details of the database access code.

Page 5: Awkward spring repositories

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

An example with Spring Data Repositories

Built with the powerful Spring boot

Page 7: Awkward spring repositories

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

The problemLet’s look at the examples

Page 9: Awkward spring repositories

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
Page 11: Awkward spring repositories

Would it not be great if we could extract those filters in smaller reusable pieces?

Page 12: Awkward spring repositories

The solutionJPA Specifications

Page 13: Awkward spring repositories

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

The solutionQueryDSL

Page 15: Awkward spring repositories

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

QueryDSL gives you all sort of good stuffs: select, from, innerJoin, join,

leftJoin, rightJoin, on, where, groupBy, having, orderBy…!

Page 17: Awkward spring repositories

The solutionStream API

Page 18: Awkward spring repositories

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

Thank youhttps://github.com/StevenNdaye/i-code-java-conf-2016!

@StevenNdaye