springspring-bootcriteriacriteria-apinhibernate-criteria

How do Specifications with Spring Data JPA really work?


For example, I have helper class with Specifications. Here is the example of one:

    public static Specification<Player> filterByName(String name) {
return ((root, query, criteriaBuilder) ->
        name == null ? null :  criteriaBuilder.like(root.get("name"), "%" + name + "%"));
    }

I use them later to call Repository. I don't understand how objects of root, query, criteriaBuiler "appear" in he method above if the are not being created by me.

I do appreciate your answer a lot!


Solution

  • This happens because from your declaration your method has to return an object implementing the Specification interface that contains the abstract toPredicate method having three parameters. What Java compiler does is the type inference of the returned value and of the three arguments from the lambda expression assigning type to them according to the toPredicate method's declaration, ending this process with success.