使用 ExampleMatcher 和 Pageable 检索数据的 JPQL 自定义查询

JPQL Custom Query to Retrieving data with ExampleMatcher & Pageable

如何使用 ExampleMatcher 使用 JPQL 自定义查询?

我正在尝试从 parent 尺寸中检索 child。 由于不允许我在 child 端添加其他方法。

我需要过滤和分页以显示 child,因为 child 包含 so-many 行数据。

这是我的资料库。

@Repository
public interface ParentRepository extends JpaRepository<Parent, String> {

    @Query(value = "SELECT c FROM Child c where c.parent.id =:id")  
    public List<Child> findChildById(String id, Example example, Pageable pageable);

}

注意:如果没有示例和可分页参数,此查询工作正常。

那个方法给我一个错误:

    Error creating bean with name 'parentRepository': 
        Invocation of init method failed; nested exception is java.lang.IllegalStateException: 
        Using named parameters for method public abstract java.util.List com.example.test.ParentRepository
.findChildById(java.lang.String,org.hibernate.criterion.Example,org.springframework.data.domain.Pageable) 
        but parameter 'Optional[example]' not found in annotated query 
        'SELECT c FROM Child c where c.parent.id =:id'!

您传递给带有 @Query 注释的方法的参数应该在查询中使用,在您的示例中,这应该是这样的:

@Query(value = "SELECT c FROM Child c where c.parent.id =:id")  
public List<Child> findChildById(@Param("id") String id);