I am trying to use interface base projection with JPA named methods and add specification, I read github issue and there the mod indicated that as of spring data jpa 3 we can use projection and specification dynamically. I tried different ways but ended up failing
public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
User findUserByUserNameIgnoreCase(String username);
<T> T findById(Long id, Class<T> type); //works
<T> List<T> findAllBy(Class<T> type); //works
<T> List<T> findAllBy(Specification<?> specification, Class<T> type); //failed
List<UserInfoProjection> findAllBy(Specification<User> specification); // failed
interface Specs {
static Specification<User> id(Long id) {
return (root, query, builder) -> builder.equal(root.get("id"), id);
}
}
}
method call
List<UserInfoProjection> up = userRepository.findAllBy(UserRepository.Specs.id(userID));
List<UserInfoProjection> pp = userRepository.findAllBy(UserRepository.Specs.id(userID), UserInfoProjection.class);
Error Log
Caused by: java.lang.IllegalArgumentException: At least 1 parameter(s) provided but only 0 parameter(s) present in query
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List org.backend.user.repository.interfaces.UserRepository.findAllBy(org.springframework.data.jpa.domain.Specification); At least 1 parameter(s) provided but only 0 parameter(s) present in query