java - 使用 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'!
解决方案
您传递给带有注释的方法的参数@Query
应该在查询中使用,在您的示例中应该是这样的:
@Query(value = "SELECT c FROM Child c where c.parent.id =:id")
public List<Child> findChildById(@Param("id") String id);
推荐阅读
- php - 预订会议室;选择给定时间段内的可用房间
- javascript - 来自节点 js 服务器的错误,但未指向任何特定文件
- python - Blob 触发功能无法读取某些 Excel 文件
- javascript - ReactJS:创建一个按动态值递增的计数器类(通过道具传递并存储在状态中)
- android - getLastSignedInAccount 方法每次都返回 not null 而不是 null
- laravel - Laravel + Vuetify 错误:渲染错误:this.items.slice 不是函数”& 无效道具:预期数组,得到对象
- javascript - 点错误:输入必须全部为 1 或 2 级,但得到 2 和 0 级
- javascript - jsPDF:使用自定义字体对齐文本
- c# - 向左使用 TabStripPlacement 时 Dragablz 不可滚动
- forms - 如果子表单中的数据为空,有没有办法删除主表单数据?