没有为存储过程 get_empoyee_details 定义参数 emp_name
Parameter emp_name was not defined for stored procedure get_empoyee_details
我有点困惑,我正在尝试使用 spring 引导 JPA 调用存储过程。已创建一个简单过程并使用 JPA 的 @NamedStoredProcedureQuery 注释调用该过程。
由于某些奇怪的原因我收到了一个错误
Parameter emp_name was not defined for stored procedure
get_empoyee_details.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError
我哪里做错了,我试过 Whosebug 上的其他答案都没有帮助
这是我的实体class
import lombok.Data;
import lombok.ToString;
@Entity
@Data
@ToString
@NamedStoredProcedureQuery(name = "get_empoyee_details", procedureName = "get_empoyee_details", resultClasses = {
Employee.class }, parameters = {
@StoredProcedureParameter(name = "emp_name", mode = ParameterMode.IN, type = String.class) })
public class Employee {
@Id
@GeneratedValue
private Long id;
@Column(name = "designation")
private String designation;
@Column(name = "employee_name")
private String employeeName;
@Column(name = "employee_code")
private String employeeCode;
}
我的控制器
@RestController
public class EmployeeController {
@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
@GetMapping("/employees")
public ResponseEntity<List<Employee>> getEmp(@RequestParam(name = "name") String name) {
List<Employee> employees = entityManager.createNamedStoredProcedureQuery("get_empoyee_details")
.setParameter("emp_name", name).getResultList();
return new ResponseEntity<>(employees, HttpStatus.OK);
}
}
提前致谢
堆栈跟踪
Error preparing registered callable parameter] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: Parameter emp_name was not defined for stored procedure get_empoyee_details.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234) ~[mssql-jdbc-8.2.2.jre8.jar:na]
我的 sql 服务器的存储过程
CREATE PROCEDURE get_empoyee_details @name nvarchar(30)
AS
SELECT *
FROM employee
WHERE employee_name = @name
GO
如异常所述,参数名称错误。
com.microsoft.sqlserver.jdbc.SQLServerException:
Parameter emp_name was not defined forstored procedure get_empoyee_details.
将其更改为名称:
@NamedStoredProcedureQuery(name = "get_empoyee_details",
procedureName = "get_empoyee_details",
resultClasses = { Employee.class },
parameters = {
@StoredProcedureParameter(name = "name", // this was wrong
mode = ParameterMode.IN, type = String.class) })
public class Employee {
我有点困惑,我正在尝试使用 spring 引导 JPA 调用存储过程。已创建一个简单过程并使用 JPA 的 @NamedStoredProcedureQuery 注释调用该过程。
由于某些奇怪的原因我收到了一个错误
Parameter emp_name was not defined for stored procedure get_empoyee_details. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError
我哪里做错了,我试过 Whosebug 上的其他答案都没有帮助
这是我的实体class
import lombok.Data;
import lombok.ToString;
@Entity
@Data
@ToString
@NamedStoredProcedureQuery(name = "get_empoyee_details", procedureName = "get_empoyee_details", resultClasses = {
Employee.class }, parameters = {
@StoredProcedureParameter(name = "emp_name", mode = ParameterMode.IN, type = String.class) })
public class Employee {
@Id
@GeneratedValue
private Long id;
@Column(name = "designation")
private String designation;
@Column(name = "employee_name")
private String employeeName;
@Column(name = "employee_code")
private String employeeCode;
}
我的控制器
@RestController
public class EmployeeController {
@PersistenceContext
private EntityManager entityManager;
@SuppressWarnings("unchecked")
@GetMapping("/employees")
public ResponseEntity<List<Employee>> getEmp(@RequestParam(name = "name") String name) {
List<Employee> employees = entityManager.createNamedStoredProcedureQuery("get_empoyee_details")
.setParameter("emp_name", name).getResultList();
return new ResponseEntity<>(employees, HttpStatus.OK);
}
}
提前致谢
堆栈跟踪
Error preparing registered callable parameter] with root cause
com.microsoft.sqlserver.jdbc.SQLServerException: Parameter emp_name was not defined for stored procedure get_empoyee_details.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234) ~[mssql-jdbc-8.2.2.jre8.jar:na]
我的 sql 服务器的存储过程
CREATE PROCEDURE get_empoyee_details @name nvarchar(30)
AS
SELECT *
FROM employee
WHERE employee_name = @name
GO
如异常所述,参数名称错误。
com.microsoft.sqlserver.jdbc.SQLServerException:
Parameter emp_name was not defined forstored procedure get_empoyee_details.
将其更改为名称:
@NamedStoredProcedureQuery(name = "get_empoyee_details",
procedureName = "get_empoyee_details",
resultClasses = { Employee.class },
parameters = {
@StoredProcedureParameter(name = "name", // this was wrong
mode = ParameterMode.IN, type = String.class) })
public class Employee {