5

Since oracle.sql.ArrayDescriptor, oracle.sql.STRUCT and oracle.sql.StructDescriptor are deprecated I'm calling an Oracle stored procedure from Java using java.sql.Array and java.sql.Struct.

I'm using as a reference this question

Here is my code.

import java.sql.Array;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Struct;
import java.util.Map;

import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Repository;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.OracleConnection;

public CrearRelacionArbolResponseBean crearRelacionArbol(String mensajeTransaccion,
            CrearRelacionArbolRequestBean request) throws DBException {
        DataSource sgaDS;
        CrearRelacionArbolResponseBean objResponse = new CrearRelacionArbolResponseBean();

        Connection conn = null;

        try {
            InitialContext ctx = new InitialContext();
            sgaDS = (DataSource) ctx.lookup(propiedades.sgaJNDI);
            Array relacionArbolArray;

            if (request.getListaRelacion() != null && !request.getListaRelacion().isEmpty()) {
                conn = sgaDS.getConnection();
                Struct[] struct = new Struct[request.getListaRelacion().size()];

                int arrayIndex = 0;
                for (Relacion relacion : request.getListaRelacion()) {
                    SpCrearRelacionArbolSqlInputType relacionArbol = new SpCrearRelacionArbolSqlInputType();
                    relacionArbol.setPI_SIVCN_IDENT_SUCESOR(Integer.valueOf(relacion.getControlSucesor()));
                    relacionArbol.setPI_SIFON_IDENT_SUCESOR(Integer.valueOf(relacion.getFormularioSucesor()));
                    relacionArbol.setPI_SIARN_IDENT_SUCESOR(Integer.valueOf(relacion.getArbolSucesor()));
                    relacionArbol.setPI_SITIN_IDENT_SUCESOR(Integer.valueOf(relacion.getTipificacionSucesor()));
                    relacionArbol.setPI_SIDRC_TIPO_RELACION(relacion.getTipoRelacion());
                    Object[] objects = new Object[] {
                            relacionArbol.getPI_SIVCN_IDENT_SUCESOR(),
                            relacionArbol.getPI_SIFON_IDENT_SUCESOR(),
                            relacionArbol.getPI_SIARN_IDENT_SUCESOR(),
                            relacionArbol.getPI_SITIN_IDENT_SUCESOR(),
                            relacionArbol.getPI_SIDRC_TIPO_RELACION()
                    };
                    struct[arrayIndex++] = conn.createStruct("USRSICES.T_OBJ_SDA", objects);
                }
                relacionArbolArray = ((OracleConnection) conn).createOracleArray("T_TBL_SDA", struct);// Error SQLException

            } else {
                logger.info("Empty List");
                relacionArbolArray = null;
            }

            logger.info(mensajeMetodo + nombreMetodo + " en DB " + propiedades.sgaDB + ", con JNDI = ["
                    + propiedades.sgaJNDI + "]");
            sgaDS.setLoginTimeout(Integer.parseInt(propiedades.timeOutConecMaxTimeCrearRelacionArbol));
            SimpleJdbcCall jdbcCall = new SimpleJdbcCall(sgaDS).withoutProcedureColumnMetaDataAccess()
                    .withSchemaName(propiedades.sgaDBOwner).withProcedureName(propiedades.spCrearRelacionArbol)
                    .declareParameters(new SqlParameter("PI_SIFON_IDENT_PREDECESOR", OracleTypes.VARCHAR),
                            new SqlParameter("PI_SIVCN_IDENT_PREDECESOR", OracleTypes.VARCHAR),
                            new SqlParameter("PI_SIARN_IDENTIFICADOR", OracleTypes.VARCHAR),
                            new SqlParameter("PI_SIDRN_NIVEL", OracleTypes.VARCHAR),
                            new SqlParameter("PI_LISTA_RELACION", OracleTypes.ARRAY),
                            new SqlParameter("PI_SIGEV_DESCRIPCION", OracleTypes.VARCHAR),
                            new SqlParameter("PI_SIDRV_USUARIO", OracleTypes.VARCHAR),
                            new SqlOutParameter("PO_CODERROR", OracleTypes.VARCHAR),
                            new SqlOutParameter("PO_DESCERROR", OracleTypes.VARCHAR));

            jdbcCall.getJdbcTemplate()
                    .setQueryTimeout(Integer.parseInt(propiedades.timeOutExecMaxTimeCrearRelacionArbol));

            SqlParameterSource objParametrosIN = new MapSqlParameterSource()
                    .addValue("PI_SIFON_IDENT_PREDECESOR", request.getIdFormulario())
                    .addValue("PI_SIVCN_IDENT_PREDECESOR", request.getIdPadre())
                    .addValue("PI_SIARN_IDENTIFICADOR", request.getIdArbol())
                    .addValue("PI_SIDRN_NIVEL", request.getNivelPadre())
                    .addValue("PI_LISTA_RELACION", relacionArbolArray)
                    .addValue("PI_SIGEV_DESCRIPCION", request.getEstado())
                    .addValue("PI_SIDRV_USUARIO", request.getUsuario());

            Map<String, Object> resultMap = jdbcCall.execute(objParametrosIN);

            objResponse.setCodigoRespuesta(utilitarios.isNullOrBlankToString(resultMap.get("PO_CODERROR")));
            objResponse.setMensajeRespuesta(utilitarios.isNullOrBlankToString(resultMap.get("PO_DESCERROR")));

        } catch (Exception e) { 
            logger.error(e,e);
            throw new DBException(codError, msjError, e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    logger.info(mensajeMetodo + "Ocurrio un error al tratar de cerrar la conexion " + e);
                }
            }
        }
        return objResponse;
    }

The full exception is as follows

Fail to convert to internal representation: weblogic.jdbc.wrapper.Struct_oracle_sql_STRUCT@28

java.sql.SQLException: Fallo al convertir a representación interna: weblogic.jdbc.wrapper.Struct_oracle_sql_STRUCT@28 at oracle.jdbc.oracore.OracleTypeADT.toDatum(OracleTypeADT.java:339) at oracle.jdbc.oracore.OracleTypeADT.toDatumArray(OracleTypeADT.java:372) at oracle.jdbc.oracore.OracleTypeUPT.toDatumArray(OracleTypeUPT.java:108) at oracle.sql.ArrayDescriptor.toOracleArray(ArrayDescriptor.java:1321) at oracle.sql.ARRAY.(ARRAY.java:141)

1 Answer 1

0

The new approach is to unwrap the OracleConnection and use createOracleArray.

OracleConnection oracleConnection = getJdbcTemplate().getDataSource().getConnection().unwrap(OracleConnection.class);
Array array = oracleConnection.createOracleArray("REF_TYPE_ARRAY", referralTypeId);

inputParameters.put("itemIds", array); 

Also see this thread: How to create an oracle.sql.ARRAY object?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.