Spring Data JPA

Suppose you have the Book entity using Spring Boot Data JPA.

@Entity
class Book(
    @Id
    val id: Int,
    val title: String,
    val author: String
)

And you need to filter by the author and the title. You can create an instance of the Criteria class and define the filters with the values.

val criteria = Criteria(
    filters = listOf(
        Filter("title", "Kotlin", FilterOperator.CONTAINS),
        Filter("author", "Svetlana Isakova", FilterOperator.EQUALS)
    )
)

This instance of the Criteria class will filter by books with title that contains the "Kotlin" word and by the author "Svetlana Isakova"

To perform a query using this criteria you will need to implement on our JPA Repository the JpaSpecificationExecutor<T> interface for allow to execute queries through the spring Specification<T> interface.

@Repository
interface BookRepository : CrudRepository<Book, Long>, JpaSpecificationExecutor<Book>

Now we can autowire BookRepository and execute the query with our criteria instance.

How to add joins on criteria

Now imagine you have the BookAuthor entity that is related with the Book entity with a OneToMany relationship

We can add a join from the Book entity to BookAuthor entity for create a criteria that filters by the author name. For this we need to create a join map.

The join map is simple, you need to define a Map with a key that is the relation name and as value the join definition.

As the last example we need to call the adapt method from CriteriaJPASpecificationAdapter and add the join map on the adapt method

If you have a FieldMap you can add it into the CriteriaJPASpecificationAdapter constructor.

Last updated