使用分页的Spring JPA存储库中的自定义查询 [英] Custom Query in Spring JPA Repository with Pagination
问题描述
我试过用Spring Boot实现JPA Repository,它工作正常。
现在,如果我尝试在使用@Query注释扩展JpaRepository的接口中实现自定义查询,它将正常返回Bean列表(使用NamedQuery)。
现在,当我尝试为自定义方法/查询使用分页时,它不起作用。
I have tried implementing JPA Repository with Spring Boot it works fine. Now if i try to implement custom query in interface which extends JpaRepository using @Query Annotation it works fine returns List of beans.(using NamedQuery). Now when i try to use pagination for custom method/query it doesn't work.
代码:
Controller:
Controller :
@RequestMapping("/custompages/{pageNumber}")
public String getAllEmployeesUsingNamedQueryWithPaging(@PathVariable Integer pageNumber,Model model)
{
Page<Employee> page = employeeService.getAllEmployeesUsingNamedQueryWithPaging(pageNumber);
System.out.println("current page "+page);
System.out.println("current page content"+page.getContent());
int current = page.getNumber() + 1;
int begin = Math.max(1, current - 5);
int end = Math.min(begin + 10, page.getTotalPages());
model.addAttribute("empList", page.getContent());
model.addAttribute("empPages", page);
model.addAttribute("beginIndex", begin);
model.addAttribute("endIndex", end);
model.addAttribute("currentIndex", current);
return "employeeWorkbench";
}
服务
Service
@Override
public Page<Employee> getAllEmployeesUsingNamedQueryWithPaging(Integer
pageNumber) {
PageRequest pageRequest =
new PageRequest(pageNumber - 1, PAGE_SIZE,
Sort.Direction.ASC, "id");
return
employeeDao.getAllEmployeesUsingNamedQueryWithPaging(pageRequest);
}
Dao
Dao
@Transactional
public interface EmployeeDao extends JpaRepository<Employee, Long>{
@Query(name="HQL_GET_ALL_EMPLOYEE_BY_ID")//Works Fine
public List<Employee> getEmpByIdUsingNamedQuery(@Param("empId") Long
empId);
@Query(name="HQL_GET_ALL_EMPLOYEE") //throws exception
public Page<Employee> getAllEmployeesUsingNamedQueryWithPaging(Pageable
pageable);
}
NamedQuery
NamedQuery
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN"
"http://hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<query name="HQL_GET_ALL_EMPLOYEE">from Employee</query>
<query name="HQL_GET_ALL_EMPLOYEE_BY_ID">from Employee where id =
:empId</query>
</hibernate-mapping>
异常:java.lang.IllegalArgumentException:为$ b $指定的类型b TypedQuery [java.lang.Long]与查询返回类型
[class com.mobicule.SpringBootJPADemo.beans.Employee]不兼容
我只想为Spring JPA Repository提供自定义方法和查询提供的分页功能。
我该如何做到这一点?
I just want to have pagination functionality provided by Spring JPA Repository for custom methods and query also. How can I achieve this?
推荐答案
我不知道为什么,但由于某种原因干脆来自Entity
会导致返回id,而您需要提供select中返回的实体,如 select f from Foo f $ c
$ b
I'm not sure why, but for some reason simply doing from Entity
causes the "id" to be returned, instead you need to provide the entity returned in the select, like select f from Foo 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 );
}
我收到同样的错误,Foo的
。我也相信你可以按照你的名字将它们引用到xml文件中。 这里是我的完整代码
I received the same error, with just from Foo
. I also believe you can reference these by name to the xml file as you were. here's my full code
进一步的测试表明,来自Foo f 的也适用,我不知道为什么需要别名,也许它是JPQL规范的一部分。
further testing says that from Foo f
also works, I do not know why the alias is required, perhaps it is part of the JPQL spec.
这是一个测试,显示如何进行简单的分页,按一个属性排序和按多个排序属性
Here is a test showing how to do simple paging, sorting by one property and sorting by multiple properties
@Test
public 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" ) );
}
这篇关于使用分页的Spring JPA存储库中的自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!