gpt4 book ai didi

java - 尝试使用 Oracle + Spring Data Jpa 查询存储过程(存储函数)时出现错误

转载 作者:行者123 更新时间:2023-12-01 17:44:16 25 4
gpt4 key购买 nike

我有创建存储函数的脚本。


CREATE OR REPLACE TYPE OBJ_EMPL AS OBJECT
(
employeeID NUMBER(19,0),
address VARCHAR2(100 CHAR),
first_Name VARCHAR2(50 CHAR),
last_Name VARCHAR2(50 CHAR)

);


CREATE OR REPLACE TYPE LIST_OBJ_EMPL IS TABLE OF OBJ_EMPL;

CREATE OR REPLACE FUNCTION GET_ALL_EMPLOYEES RETURN LIST_OBJ_EMPL IS

varObjEmpl LIST_OBJ_EMPL;


BEGIN
SELECT OBJ_EMPL (
employeeID,
first_Name,
last_Name,
address
)

BULK COLLECT INTO varObjEmpl
FROM EMPLOYEES;

RETURN varObjEmpl;
EXCEPTION
WHEN NO_DATA_FOUND THEN
return NULL;
WHEN OTHERS THEN
-- Consider logging the error and then re-raise
RAISE;
END GET_ALL_EMPLOYEES;
/


--SELECT * FROM TABLE(getListEmployees);



/*

CREATE TABLE EMPLOYEES
(EMPLOYEEID NUMBER(19,0) NOT NULL ENABLE,
ADDRESS VARCHAR2(100 CHAR),
FIRST_NAME VARCHAR2(50 CHAR),
LAST_NAME VARCHAR2(50 CHAR),
CONSTRAINT PK_EMPLOYEEID PRIMARY KEY (EMPLOYEEID)
);

*/

  • 实体
@Entity
@Table(name = "employees")
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(name = "getListEmployees",
procedureName = "get_all_employees",
resultClasses = Employee.class)
})
public class Employee {

@Id
@SequenceGenerator(name = "jpaSequence.Employee",
sequenceName = "SEQUENCE_EMPLOYEE",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "jpaSequence.Employee")
@Column(name = "employeeID")
private Long employeeID;

@Column(name = "first_Name")
@Size(message = "Field 'firstName' can't be более {} ",max = 50)
private String firstName;

@Column(name = "last_Name")
@Size(message = "Field 'firstName' can't be более {} ",max = 50)
private String lastName;

@Column(name = "address")
@Size(message = "Field 'firstName' can't be более {} ",max = 100)
private String address;

public Employee() {
}

  • 存储库
public interface EmployeesRepository
extends CrudRepository<Employee, Long>,
EmployeesRepositoryCustom {
}
public interface EmployeesRepositoryCustom {
List<Employee> getAllEmployees();
}
@Repository
public class EmployeesRepositoryImpl implements EmployeesRepositoryCustom {

@PersistenceContext
private EntityManager entityManager;

@Override
public List<Employee> getAllEmployees() {

StoredProcedureQuery getAllEmployees =
entityManager.createNamedStoredProcedureQuery("getListEmployees");

return getAllEmployees.getResultList();
}
}

当我尝试获取数据时出现异常。

Error calling CallableStatement.getMoreResults; SQL [get_all_employees]; nested exception is org.hibernate.exception.SQLGrammarException: Error calling CallableStatement.getMoreResults

java.sql.SQLException: ORA-06550: Row 1, column 7: PLS-00221: 'GET_ALL_EMPLOYEES' is not a procedure or is not defined ORA-06550: Row 1, column 7: PL/SQL: Statement ignored

也许您无法使用 @NamedStoredProcedureQuery 注释来处理存储函数?我不知道该怎么办。谁有什么想法?

最佳答案

解决方案

public interface EmployeesRepository
extends CrudRepository<Employee, Long>,
EmployeesRepositoryCustom {

String query = "select * from table (get_all_employees)";

@Query(nativeQuery = true, value = query)
Iterable<Employee> findAllEmployees();

}

关于java - 尝试使用 Oracle + Spring Data Jpa 查询存储过程(存储函数)时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60889206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com