Spring JPA Pagination on Sub-Entities using Dynamic Projection

1k Views Asked by At

How to Paginate on the ChildEntity from a ParentEntity (unidirectional oneToMany) via Dynamic Projection. Heres my code sample which works while paginating on Parent .

Repository - I tried this, looking this response , but it throws

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dumyRepository': Invocation of init method failed; nested exception is java.lang.IllegalStateException: Using named parameters for method public abstract org.springframework.data.domain.Page com.api.repo.DumyRepository.findByDumyIdIn(java.util.Set,java.lang.Class,org.springframework.data.domain.Pageable) but parameter 'Optional[dumyIds]' not found in annotated query 'select child from ParentEntity p inner join p.childEntities child where p = :type'!

 `@Query("select child from ParentEntity p inner join p.childEntities child where p = :type")
<T> Page<T> findByDumyIdIn(Set<String> demoIds, @Param("type") Class<T> type, Pageable pagable);` 

May be i'm missing something here ?

@Repository
public interface ParentRepository extends
        JpaRepository<ParentEntity, Long> {

    <T> Page<T> findByDumyIdIn(Set<String> dumyIds, Class<T> type,Pageable pagable);

}

Projection Interfaces

public interface ChildDetailsView {
    String getDumyId();
    List<ChildEntity> getChildEntities();
}

public interface ParentDetailsView {
    Long getId();
    String getDumyId();
    String getDc();
    String getComp();
}

Service Classes

public Map<String, ParentDetailsView> findParentMapping(Set<String> dumyIds, Pageable pageable) {
        return this.dsRepo.findByDumyIdIn(demoIds,ParentDetailsView.class,pageable)
                .stream()
                .collect(Collectors.toMap( k  -> k.getDumyId(), v->v));
    }


 public Map<String, ChildDetailsView> findChildMapping(Set<String> dumyIds,
                                                              Pageable pageable) {
        return this.dsRepo.findByDumyIdIn(demoIds, ChildDetailsView.class,pageable)
                .stream()
                    .collect(Collectors.toMap( k  -> k.getDumyId(), v->v));
        }

Entity Classes

@Entity
public class ParentEntity{
    @Id
    private Integer id;

    @NonNull
    @Column(nullable = false, length = 31)
    private String dumyId;

    @NonNull
    @Column(nullable = false, length = 31)
    private String dc;

    @NonNull
    @Column(nullable = false, length = 31)
    private String comp;

    @OneToMany
    private List<ChildEntity> childEntities;
}

@Entity
public class ChildEntity {
    @Id
    private Integer id;

    @NonNull
    private Sting name;
}
0

There are 0 best solutions below