簡體   English   中英

Oracle Java存儲函數 - ORA-00932:不一致的數據類型

[英]Oracle Java Stored Function - ORA-00932: inconsistent datatypes

試圖調用java存儲函數並獲取一個java對象並將其轉換為oracle類型。 這是一個10g的數據庫,我們使用的是Java 1.4.2。 (我知道升級是答案,不幸的是我受限於這種環境。)

下面列出的代碼和錯誤。

任何幫助將不勝感激。

TestMapping.java

public class TestMapping {
    public static TestObject getResponse(){
        TestObject testObject = new TestObject();
        testObject.setMyTestValue("TEST");

        return testObject;
    }
}

TestObject.java

import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class TestObject implements SQLData {
    private String myTestValue;

    private String sqlTypeName;
    public String getSQLTypeName() throws SQLException {
        return sqlTypeName;
    }

    public void readSQL(SQLInput stream, String typeName) throws SQLException {
        sqlTypeName = typeName;
        myTestValue = stream.readString();
    }

    public void writeSQL(SQLOutput stream) throws SQLException {
        stream.writeString(myTestValue);
    }

    public String getMyTestValue() {
        return myTestValue;
    }

    public void setMyTestValue(String myTestValue) {
        this.myTestValue = myTestValue;
    }
}

命令行:

javac TestObject.java
javac TestMapping.java
loadjava -user user/pass@server:1521:instance -t -r TestObject.class
loadjava -user user/pass@server:1521:instance -t -r TestMapping.class

Oracle類型:

create or replace
type test_object  as object(
myTestValue  varchar2(50));

發布Java函數:

create or replace
FUNCTION test_mapping RETURN test_object
AS LANGUAGE JAVA
NAME 'TestMapping.getResponse() return TestObject';

匿名阻止測試:

set serveroutput on;
declare 
  response test_object; 
begin
  response := test_mapping();
  DBMS_OUTPUT.PUT_LINE('myTestValue : ' || response.myTestValue);
end;

錯誤:

Error report:
ORA-00932: inconsistent datatypes: expected a return value that is an instance of a user defined Java class convertible to an Oracle type got an object that could not be converted
ORA-06512: at "USER.TEST_MAPPING", line 1
ORA-06512: at line 4
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:

您需要返回oracle.sql.STRUCT的實例,而不是自定義Java類型的實例。 這在Oracle出版物中都是如此......

create or replace
FUNCTION test_mapping RETURN test_object
AS LANGUAGE JAVA
NAME 'TestMapping.getResponse() return oracle.sql.STRUCT';

......在Java類中......

public static oracle.sql.STRUCT getResponse(){ ...

要構建getResponse()必須返回的STRUCT,您需要執行以下操作:

// This is the object you really want to pass back
TestObject result;

STRUCT oracleResult;
try{
  StructDescriptor resultStructDescriptor = StructDescriptor.createDescriptor("TEST_OBJECT", yourOracleConnection);

  Object[] attributes = {result.myTestValue};
  oracleResult = new STRUCT(resultStructDescriptor, yourOracleConnection, attributes);
} catch ( SQLException e ) {
    throw new RuntimeException(e);
}
return oracleResult;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM