如何使用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'!
最佳答案
传递给用@Query
注释的方法的参数应该在查询中使用,在您的示例中应该是这样的:
@Query(value = "SELECT c FROM Child c where c.parent.id =:id")
public List<Child> findChildById(@Param("id") String id);
关于java - 使用 ExampleMatcher 和 Pageable 检索数据的 JPQL 自定义查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54996601/