首页 > 解决方案 > 使用 ExampleMatcher 和 Pageable 检索数据的 JPQL 自定义查询

问题描述

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

我正在尝试从父母大小中检索孩子。由于我不允许在子端添加另一种方法。

我需要过滤和分页以显示孩子,因为孩子包含太多行数据。

这是我的存储库。

@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'!

标签: javaspringhibernatespring-data-jpajpql

解决方案


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

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

推荐阅读