简体   繁体   English

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

[英]JPQL Custom Query to Retrieving data with ExampleMatcher & Pageable

How to using JPQL Custom query using ExampleMatcher? 如何使用ExampleMatcher使用JPQL自定义查询?

I'm trying to retrieve the child from parent size. 我正在尝试从父母的大小中检索孩子。 Since I'm not allowed to add another method on the child side. 由于不允许在孩子方面添加其他方法。

I need to filter and paging for showing the child because the child contains so-many row data. 我需要过滤和分页以显示子项,因为子项包含许多行数据。

This is my repository. 这是我的资料库。

@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);

}

Note: This query is working fine, if without example and pageable as parameters. 注意:如果没有示例并且该参数可分页,则此查询工作正常。

That method give me the an error : 该方法给我一个错误:

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

Parameters you pass to method annotated with @Query should be used in query, in your example this should be something like this: 传递给使用@Query注释的方法的参数应在查询中使用,在您的示例中,该参数应类似于以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM