I am facing difficulty while fetching Complex Object Type from Oracle to Java.
I am using Spring JDBCTemplate and Spring StoredProcedure.
At the Oracle end I have used a function which return an OBJECT(UNIT)which consists of TABLE(DIVISION_TAB) of an OBJECT(DIVISION) which itself consists of a TABLE(EMPLOYEE_TAB) of another OBJECT(EMPLOYEE).
These types are defined as below:
create or replace TYPE UNIT AS OBJECT
(
UNIT_ID NUMBER(38),
UNIT_NAME VARCHAR(50),
DIVIVSION_LIST DIVIVSION_TAB
)
create or replace
TYPE DIVISION_TAB
AS TABLE OF DIVISION
create or replace
TYPE DIVISION AS OBJECT
(
DIV_ID NUMBER(38),
DIV_NAME VARCHAR(50),
DIV_STATUS NUMBER(38),
EMPLOYEE_LIST EMPLOYEE_TAB
)
create or replace
TYPE EMPLOYEE_TAB
AS TABLE OF EMPLOYEE
create or replace
TYPE EMPLOYEE AS OBJECT
(
EMP_ID NUMBER(38),
EMP_NAME VARCHAR(50),
EMP_STATUS NUMBER(38),
EMP_SAL NUMBER(20),
)
Function at Database level return me an SQL Object of UNIT, which I am using in the following way while retrieving it in Java.
declareParameter(new SqlOutParameter("UNIT", OracleTypes.STRUCT, "UNIT",new SqlReturnType() {
public Object getTypeValue(CallableStatement callableStatement, int colIndx, int sqlType, String typeName)
throws SQLException {
Connection connection = callableStatement.getConnection();
Map<String, Class<?>> typeMap = connection.getTypeMap();
typeMap.put("UNIT", Unit.class);
typeMap.put("DIVISION_TAB", java.sql.Array.class);
typeMap.put("DIVISION", Division.class);
typeMap.put("EMPLOYEE_TAB", java.sql.Array.class);
typeMap.put("EMPLOYEE", Employee.class);
STRUCT struct = (STRUCT)callableStatement.getObject(colIndx);
Object[] attr = struct.getAttributes();
return attr;
}
}));
PROBLEM::::
When I am debugging Object[] returned by SqlReturnType() method then I am able to fetch the details like UNIT_ID and UNIT_NAME but on the 3rd attribute i.e of type oracle.sql.ARRAY, when I am performing the following operation :
ARRAY array = (ARRAY)attr[3];
Object[] objects = (Object[])((ARRAY)attr[3]).getArray();
it throws SQL Internat Exception.
This problem was resolved. Just a simple mistake. The code is perfectly correct, I just missed giving grant to the TYPES I have defined.