Spring 带有分页的 JPA 存储库中的自定义查询
Custom Query in Spring JPA Repository with Pagination
我已经尝试使用 Spring 启动 JPA 存储库,它工作正常。
现在,如果我尝试在使用 @Query 注释扩展 JpaRepository 的接口中实现自定义查询,它工作正常 returns beans 列表。(使用 NamedQuery)。
现在,当我尝试使用自定义分页时 method/query 它不起作用。
代码:
控制器:
@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";
}
服务
@Override
public Page<Employee> getAllEmployeesUsingNamedQueryWithPaging(Integer
pageNumber) {
PageRequest pageRequest =
new PageRequest(pageNumber - 1, PAGE_SIZE,
Sort.Direction.ASC, "id");
return
employeeDao.getAllEmployeesUsingNamedQueryWithPaging(pageRequest);
}
道
@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);
}
命名查询
<?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>
Exception : java.lang.IllegalArgumentException: Type specified for
TypedQuery [java.lang.Long] is incompatible with query return type
[class com.mobicule.SpringBootJPADemo.beans.Employee]
我只想让 Spring JPA Repository 为自定义方法和查询提供分页功能。
我怎样才能做到这一点?
我不确定为什么,但出于某种原因,只需执行 from Entity
就会导致返回 "id",而您需要提供 select 中返回的实体,喜欢 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 );
}
我收到了同样的错误,只是 from Foo
。我也相信您可以像以前一样按名称将这些引用到 xml 文件。 here's my full code
进一步测试表明 from Foo f
也有效,我不知道为什么需要别名,也许它是 JPQL 规范的一部分。
这是一个测试,展示了如何进行简单的分页、按一个排序 属性 和按多个属性排序
@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 存储库,它工作正常。 现在,如果我尝试在使用 @Query 注释扩展 JpaRepository 的接口中实现自定义查询,它工作正常 returns beans 列表。(使用 NamedQuery)。 现在,当我尝试使用自定义分页时 method/query 它不起作用。
代码:
控制器:
@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";
}
服务
@Override
public Page<Employee> getAllEmployeesUsingNamedQueryWithPaging(Integer
pageNumber) {
PageRequest pageRequest =
new PageRequest(pageNumber - 1, PAGE_SIZE,
Sort.Direction.ASC, "id");
return
employeeDao.getAllEmployeesUsingNamedQueryWithPaging(pageRequest);
}
道
@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);
}
命名查询
<?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>
Exception : java.lang.IllegalArgumentException: Type specified for TypedQuery [java.lang.Long] is incompatible with query return type [class com.mobicule.SpringBootJPADemo.beans.Employee]
我只想让 Spring JPA Repository 为自定义方法和查询提供分页功能。 我怎样才能做到这一点?
我不确定为什么,但出于某种原因,只需执行 from Entity
就会导致返回 "id",而您需要提供 select 中返回的实体,喜欢 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 );
}
我收到了同样的错误,只是 from Foo
。我也相信您可以像以前一样按名称将这些引用到 xml 文件。 here's my full code
进一步测试表明 from Foo f
也有效,我不知道为什么需要别名,也许它是 JPQL 规范的一部分。
这是一个测试,展示了如何进行简单的分页、按一个排序 属性 和按多个属性排序
@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" ) );
}