首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >findAll(Specifications<T>,Pageable)方法不能用于JpaSpecificationExecutor<T>存储库吗?

findAll(Specifications<T>,Pageable)方法不能用于JpaSpecificationExecutor<T>存储库吗?
EN

Stack Overflow用户
提问于 2018-01-17 09:41:06
回答 1查看 9.6K关注 0票数 4

不能对findAll存储库使用JpaSpecificationExecutor (规范,可访问)方法。我将存储库接口设置为:

代码语言:javascript
复制
package com.task.task.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import com.task.task.domain.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>, JpaSpecificationExecutor<Employee> {
}

每当我调用employeeRepository.findAll(specifications,分页时);将引发此错误:

代码语言:javascript
复制
    "error": "Internal Server Error",
    "exception": "org.springframework.beans.BeanInstantiationException",
    "message": "Failed to instantiate [org.springframework.data.jpa.domain.Specifications]: No default constructor found;
nested exception is java.lang.NoSuchMethodException: org.springframework.data.jpa.domain.Specifications.<init>()",
    "path": "/api/task/employee"

这是堆栈跟踪:

代码语言:javascript
复制
2018-01-17 14:41:29.816 ERROR 12132 --- [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.jpa.domain.Specifications]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.data.jpa.domain.Specifications.<init>()] with root cause

java.lang.NoSuchMethodException: org.springframework.data.jpa.domain.Specifications.<init>()
        at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_144]
        at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_144]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:102) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

完整代码:https://github.com/SanketKD/SpecificationExecutor

实体:

代码语言:javascript
复制
@Entity
@Table(name = "emp111")
public class Employee {

  @Id
  @Column(name = "employee_id")
  private Long employeeId;

  @Column(name = "ename", length = 20)
  private String ename;

  @Column(name = "hire_date")
  private Date hireDate;

  @Column(name = "salary")
  private Long salary;

  @Column(name = "skills", length = 30)
  private String skills;

// getters setters

服务:

代码语言:javascript
复制
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;

import com.task.task.domain.Employee;
import com.task.task.repository.EmployeeRepository;

@Service
public class EmployeeService {

  private final EmployeeRepository employeeRepository;

  @Autowired
  public EmployeeService(EmployeeRepository employeeRepository) {
    this.employeeRepository = employeeRepository;
  }

  public Employee getEmployee(Long employeeId) {
    return employeeRepository.findOne(employeeId);
  }

  public Page<Employee> getEmployees(Specifications<Employee> specifications, Pageable pageable) {
    return employeeRepository.findAll(specifications, pageable);
  }
}

主计长:

代码语言:javascript
复制
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.task.task.domain.Employee;
import com.task.task.service.EmployeeService;

@RestController
@RequestMapping("api/task/employee")
public class EmployeeController {

  private final EmployeeService employeeService;

  @Autowired
  public EmployeeController(EmployeeService employeeService) {
    this.employeeService = employeeService;
  }

  @RequestMapping(method = RequestMethod.GET, path = "/{employeeId:[0-9]\\d*}")
  public Employee getEmployee(@PathVariable Long employeeId) {
    return this.employeeService.getEmployee(employeeId);
  }

  @RequestMapping(method = RequestMethod.GET)
  public Page<Employee> getEmployees(Specifications<Employee> specifications, Pageable pageable) {
    return this.employeeService.getEmployees(specifications, pageable);
  }
}

储存库:

代码语言:javascript
复制
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

import com.task.task.domain.Employee;

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>, JpaSpecificationExecutor<Employee> {
}
EN

回答 1

Stack Overflow用户

发布于 2018-01-17 10:16:32

您得到的异常与存储库无关,这是因为您在控制器中映射了Specification<Employee>。这是不可能的,因为Spring不知道如何将传入的请求“解析”到Specification<Employee>。它将试图通过调用默认构造函数来构造Specification,但是由于没有默认构造函数,因此它会抛出一个异常。

与其在控制器中映射它,还需要使用适当的请求体(或参数),并在控制器或服务中创建Specification

使用Spring引导2.x.x,您可以使用以下方法实现:

代码语言:javascript
复制
@RequestMapping(method = RequestMethod.GET)
public Page<Employee> getEmployees(
        // Just plain parameters
        @RequestParam String name,
        @RequestParam int page,
        @ResuestParam int limit) {
    // Creating the specification
    Specification<Employee> spec = Specification.where(EmployeeSpecs.employeeName(name));
    // Creating the Pageable as well
    Pageable pageable = PageRequest.of(page, limit);
    return this.employeeService.getEmployees(specifications, pageable);
}

使用Spring 1.x.xSpecification.where()称为Specifications.where()。此外,PageRequest.of(..)静态方法不存在,您应该使用new PageRequest(..)构造函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48297885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档