In my application I have some entities which are extended from User class. User has firstName and lastName fields. There is a search for each entity extended from User. To implement search I'm using Criteria API. As we are talking about the same params, queries for them look very similar. So I decided to create generic class to gather everything in one place
public abstract class NameSpecificationManager<Entity, SearchDto extends BasicSearchDto, MetaModel extends User_>
extends SpecificationManager<Entity, SearchDto> {
protected final SpecificationBuilder<Entity> entityByFirstName = (final String firstName) ->
(root, query, cb) -> cb.like(root.get(MetaModel.firstName), getParameterPattern(firstName));
protected final SpecificationBuilder<Entity> entityByLastName = (final String lastName) ->
(root, query, cb) -> cb.like(root.get(MetaModel.lastName), getParameterPattern(lastName));
}
Here is SpecificationBuilder:
public interface SpecificationBuilder<Entity> {
Specification<Entity> getSpecOfSearchParam(String searchParam);
}
The problem is that MetaModel.lastName and MetaModel.firstName are underlined by Intellij (it says that "Cannot resolve method with ...User.firstName"). May be it's because it's not clear what exact extension of User class we are expecting. May be there is a way to avoid this. Thanks in advance.