gpt4 book ai didi

java - 具有从 Hibernate 调用的 OUT 参数的 Oracle 存储过程

转载 作者:行者123 更新时间:2023-12-01 12:18:02 28 4
gpt4 key购买 nike

我通过 Hibernate 调用带有 OUT 参数的存储过程并收到以下错误:

17:30:52,646 ERROR [STDERR] Caused by: java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'GET_ACTIVITY_RECORDS'
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'GET_ACTIVITY_RECORDS'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

我的存储过程:

  create or replace 
PACKAGE BODY "TEST_NESTEDTAB_PKG" AS

PROCEDURE GET_ACTIVITY_RECORDS( TEMP_RESULT OUT ACT_LOG_TYPE, USERNAME IN ACTIVITY_LOG.USER_NAME%TYPE) AS
BEGIN
SELECT * BULK COLLECT INTO TEMP_RESULT
FROM ACTIVITY_LOG
WHERE USER_NAME = USERNAME;
END GET_ACTIVITY_RECORDS;

END TEST_NESTEDTAB_PKG;

hibernate 映射:

 <sql-query name="getactivityLog" callable="true">
<return class="com.test.ps.domain.SPActiveLog">
<return-property name="activityId" column="ACTIVITY_ID" />
<return-property name="username" column="USER_NAME" />
<return-property name="activityDate" column="ACTIVITY_DATE" />
<return-property name="activity" column="ACTIVITY" />
<return-property name="userId" column="USERID" />
<return-property name="action" column="ACTION" />
<return-property name="userRole" column="USER_ROLE" />
<return-property name="description" column="DESCRIPTION" />
<return-property name="category" column="CATEGORY" />
<return-property name="result" column="RESULT" />
</return>
{ call TEST_NESTEDTAB_PKG.GET_ACTIVITY_RECORDS(?,:USERNAME) }
</sql-query>

我按如下方式调用存储过程:

    Query qry = getSession().getNamedQuery("getactivityLog");       
qry.setParameter("USERNAME", "FACTORY TOPTAR TOTAIN");
Object result = qry.list();

谁能解释一下这段代码有什么问题吗?

最佳答案

试试这个:

{ ? = call TEST_NESTEDTAB_PKG.GET_ACTIVITY_RECORDS(:USERNAME) }

文档:https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/querysql-namedqueries.html

16.2.2. Using stored procedures for querying

Hibernate 3 introduces support for queries via stored procedures and functions. Most of the following documentation is equivalent for both. The stored procedure/function must return a resultset as the first out-parameter to be able to work with Hibernate. An example of such a stored function in Oracle 9 and higher is as follows:

CREATE OR REPLACE FUNCTION selectAllEmployments
RETURN SYS_REFCURSOR
AS
st_cursor SYS_REFCURSOR;
BEGIN
OPEN st_cursor FOR
SELECT EMPLOYEE, EMPLOYER,
STARTDATE, ENDDATE,
REGIONCODE, EID, VALUE, CURRENCY
FROM EMPLOYMENT;
RETURN st_cursor;
END;

To use this query in Hibernate you need to map it via a named query.

<sql-query name="selectAllEmployees_SP" callable="true">
<return alias="emp" class="Employment">
<return-property name="employee" column="EMPLOYEE"/>
<return-property name="employer" column="EMPLOYER"/>
<return-property name="startDate" column="STARTDATE"/>
<return-property name="endDate" column="ENDDATE"/>
<return-property name="regionCode" column="REGIONCODE"/>
<return-property name="id" column="EID"/>
<return-property name="salary">
<return-column name="VALUE"/>
<return-column name="CURRENCY"/>
</return-property>
</return>
{ ? = call selectAllEmployments() }
</sql-query>

Notice stored procedures currently only return scalars and entities. and are not supported.

关于java - 具有从 Hibernate 调用的 OUT 参数的 Oracle 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26864627/

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