具有分页功能的Spring JPA存储库中的自定义查询

具有分页功能的Spring JPA存储库中的自定义查询,第1张

具有分页功能的Spring JPA存储库中的自定义查询

我不确定为什么,但是由于某种原因,简单地

from Entity
导致返回“ id”,而是需要提供select中返回的实体,例如
select f fromFoo f

public interface FooRepo extends PagingAndSortingRepository<Foo, Long> {@Query( "select f from Foo f" )Page<Foo> findAllCustom( Pageable pageable );Page<Foo> findAllByBarBazContaining( String baz, Pageable pageable );}

我收到了同样的错误,只是

fromFoo
。我也相信您可以像以前一样通过名称将其引用到xml文件。这是我的完整代码

进一步的测试表明它

from Foo f
也可行,我不知道为什么需要别名,也许它是JPQL规范的一部分。

这是一个测试,显示如何进行简单的分页,按一个属性排序和按多个属性排序

@Testpublic void testFindAllCustom() throws Exception {    Page<Foo> allCustom = fooRepo.findAllCustom( pageable );    assertThat( allCustom.getSize(), is( 2 ) );    Page<Foo> sortByBazAsc = fooRepo.findAllCustom( new PageRequest( 0, 2, Sort.Direction.ASC, "bar.baz" ) );    assertThat( sortByBazAsc.iterator().next().getBar().getBaz(), is( "2baz2bfoo" ) );    Page<Foo> complexSort = fooRepo.findAllCustom( new PageRequest( 0, 2, new Sort( new Sort.Order( Sort.Direction.DESC, "bar.baz" ), new Sort.Order( Sort.Direction.ASC, "id" )    ) ) );    assertThat( complexSort.iterator().next().getBar().getBaz(), is( "baz1" ) );}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5675985.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存