1

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
0

1 Answer 1

0

The error you are seeing is mainly caused by Spring Data JPA not being able to dynamically generate a query for the method when using projections and Specifications together in the format you have provided. While Spring Data JPA 3.0 introduced support for combining projections with specifications, there are certain limitations, especially when trying to combine them dynamically. One of the options to fix is use a Custom Implementation for Combining Specifications with Projections.

Add a Custom Repository Implementation for UserRepository:

    public interface UserRepositoryCustom {
    <T> List<T> findAllBySpecification(Specification<User> specification, Class<T> projectionType);
}

Provide the implementation:

import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;

import java.util.List;

public class UserRepositoryCustomImpl implements UserRepositoryCustom {

    private final EntityManager entityManager;

    public UserRepositoryCustomImpl(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Override
    public <T> List<T> findAllBySpecification(Specification<User> specification, Class<T> projectionType) {
        // Create CriteriaBuilder and CriteriaQuery
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<T> query = builder.createQuery(projectionType);

        // Define the root for the query
        Root<User> root = query.from(User.class);

        // Apply Specification filters
        if (specification != null) {
            query.where(specification.toPredicate(root, query, builder));
        }

        // Define query projection
        query.select(builder.construct(projectionType, root.get("id"), root.get("username"))); // Project only fields needed

        // Create and execute the query
        TypedQuery<T> typedQuery = entityManager.createQuery(query);
        return typedQuery.getResultList();
    }
}

Update UserRepository to Extend Custom Interface:

    public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User>, UserRepositoryCustom {
    User findUserByUserNameIgnoreCase(String username);
    <T> T findById(Long id, Class<T> type);
    <T> List<T> findAllBy(Class<T> type); 
}

To specify which projection to apply, use the findAllBySpecification method with both a Specification and a projection type:

List<UserInfoProjection> projections = userRepository.findAllBySpecification(
    UserRepository.Specs.id(1L), UserInfoProjection.class);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for you suggestion, It is a promising solution. I actually found a way to to use them dynamically userRepository.findBy(UserRepository.Specs.id(Arrays.asList(userID)), q->q.as(UserInfoProjection.class).all()); but JPA gives a way to perform this but I dont know why the query which is being executed fetches/contains all the properties of the class .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.