1

So, I'm trying to figure out how do I need to pass the following custom object (not included in any package) to a function call:

    CREATE OR REPLACE TYPE RABO_OWNER_DEV.SIANAGR_SEARCH_PARAM AS OBJECT
(
   pFilter        VARCHAR2(32000 CHAR),
   pOrderByCond   VARCHAR2(32000 CHAR),
   pScopeName     VARCHAR2(100 CHAR),
   ANACODANA      NUMBER(9),
   ANADENOM       VARCHAR2(180 CHAR),
   ANADENOMLANG2  VARCHAR2(180 CHAR),
   ANADENOMLANG3  VARCHAR2(180 CHAR),
   ANAFGIUR       VARCHAR2(10 CHAR),
   DSCOUNTRY      VARCHAR2(40 CHAR),
   ANACFISC       VARCHAR2(40 CHAR),
   ANAPIVA        VARCHAR2(40 CHAR),
   FROM_ANACODANA NUMBER(9),
   TO_ANACODANA   NUMBER(9),
   ANAEXCOD       VARCHAR2(30 CHAR),
   ANATIPPERS     VARCHAR2(1 CHAR),
   ANACODCAPOGRP  VARCHAR2(16 CHAR),
   ANACODDOC      VARCHAR2(10 CHAR),
   ANANUMDOC      VARCHAR2(15 CHAR),
   CLCINDTIPOIND  VARCHAR2(1 CHAR),
   CLCINDCODNAZ   VARCHAR2(10 CHAR),
   CLCINDPROV     VARCHAR2(10 CHAR),
   CLCINDLOC      VARCHAR2(100 CHAR),
   CLCINDCAP      VARCHAR2(10 CHAR),
   CLCINDIRIZZO   VARCHAR2(250 CHAR),
   CLCINDTEL      VARCHAR2(25 CHAR),
   CLCINDCELL     VARCHAR2(25 CHAR),
   CLCINDFAX      VARCHAR2(25 CHAR),
   CLCINDEMAIL    VARCHAR2(80 CHAR),
   CLCINDPEC      VARCHAR2(80 CHAR),
   CLCGRPNDG      VARCHAR2(16 CHAR),
   CLCGRPCODGRP   NUMBER(9),
   CLCGRPDESC     VARCHAR2(50 CHAR),
   MULTISEARCH    VARCHAR2(32000 CHAR),
   CLCTIPO        VARCHAR2(1 CHAR),
   ANACRISC       VARCHAR2(20 CHAR),
   ANASAE       VARCHAR2(10 CHAR),
   TELIMITCONS     NUMBER(1),
   TERICHOBLIO     NUMBER(1)
)
/

So, first I thought that it's possible to get the object dinamically from the DB and I've searched for something online and the following code is what I found (I will modify this in a future getting back the attributes, but for testing if it works I call the method as it is):

void getAttributeInfo (Connection conn, String type_name) throws SQLException 
{ 
 // get the type descriptor 
 StructDescriptor desc = StructDescriptor.createDescriptor (type_name, conn); 

 // get type meta data 
 ResultSetMetaData md = desc.getMetaData (); 

 // get # of attrs of this type 
 int numAttrs = desc.getLength (); 

 // temporary buffers 
 String attr_name; 
 int attr_type; 
 String attr_typeName; 

 System.out.println ("Attributes of "+type_name+" :"); 
 for (int i=0; i<numAttrs; i++) 
 { 
   attr_name = md.getColumnName (i+1); 
   attr_type = md.getColumnType (i+1); 
   System.out.println (" index"+(i+1)+" name="+attr_name+" type="+attr_type); 

   // drill down nested object 
   if (attr_type == OracleTypes.STRUCT) 
   { 
     attr_typeName = md.getColumnTypeName (i+1); 

     // recursive calls to print out nested object meta data 
     getAttributeInfo (conn, attr_typeName); 
   } 
 } 
}

This is my PL/SQL function:

FUNCTION LS_SEARCH_SIANAGR(
   p_CodCompany   VARCHAR2,
   p_Bo_SessionID VARCHAR2 ,
   p_Context NUMBER,
   p_RowRead      OUT NOCOPY NUMBER,
   p_RowCount     OUT NOCOPY NUMBER,
   p_NumOfPages   OUT NOCOPY NUMBER,
   p_PAGE2EXTRACT NUMBER,
   p_WithActionInfo NUMBER,
   p_UseView NUMBER,
   p_searchParam  SIANAGR_SEARCH_PARAM
) RETURN SYS_REFCURSOR
AS
  PRAGMA AUTONOMOUS_TRANSACTION;
  v_Result SYS_REFCURSOR;
BEGIN

    LsSiSession.checkSession(p_CodCompany, p_Bo_SessionID, p_Context);  

  v_Result :=  LsAnaHeader_EL.LS_SEARCH_SIANAGR(
    p_CodCompany,
    p_Bo_SessionID,
    p_Context,
    p_RowRead,
    p_RowCount,
    p_NumOfPages,
    p_PAGE2EXTRACT,
    p_WithActionInfo,
    p_UseView,
    p_searchParam);
  COMMIT;
  RETURN v_Result;
EXCEPTION
  WHEN OTHERS THEN
    ROLLBACK;
    LsGlobal.LsErrorTrace(SQLERRM);
END;

Then, I've defined the CallableStatement as this:

CallableStatement cs = null;

getAttributeInfo (conn, "SIANAGR_SEARCH_PARAM");
String anagrSearchSql = "{call myPackage.myProcedure(?,?,?,...)}";

When I try to run I get:

class java.lang.ClassCastException com.sun.proxy.$Proxy28 cannot be cast to oracle.jdbc.OracleConnection

At the following line:

StructDescriptor desc = StructDescriptor.createDescriptor (type_name, conn); 

I also searched for this error, but I only found that I have to unwrap the connection to an 'OracleConnection', but I don't find the purpose of that.(Connection cannot be cast to oracle.jdbc.OracleConnection)

This is how i defined my connection:

public LinkRABOUnitService(String id, RTXManager mgr, Element descr) throws RTXException {
    super(id, mgr, descr);

    hbService = (HibernateService) mgr.getService(dbId + ".hibernate", HibernateService.class);
}

public Object execute(Map operationContext, Map sessionContext) throws RTXException {
    Session hbSession = null;
    boolean inTransaction = false;
    Connection connection = null;
    StringBuffer response = new StringBuffer();

    /* retrieve unit parameters */
    String xml = BeanHelper.asString(operationContext.get(getId() + ".xml"));
    String idCodeElaborazione = BeanHelper.asString(operationContext.get(getId() + ".idElaborazione"));

    try {
        // get hibernate session
        hbSession = locateTransactionSession(hbService, operationContext);
        if (hbSession != null) {
            inTransaction = true;
        } else {
            hbSession = openSession(hbService, operationContext, sessionContext);
        } ....

Do I have to search another way to pass the custom object to an oracle function?

Asjon
  • 59
  • 5
  • 1
    Can you please spend some time to compress your example to a minimal reproducible case? Due to *can not cast connection* - this is offen caused by the proxy connection object used. You must find some method of the proxy to *get the real Db connection*. – Marmite Bomber Jun 29 '18 at 10:14
  • What platform are you using? You need to get the real connection and the method varies. However, in the meantime, open a connection manually (i.e. not with Hibernate or an application server or anything) and see if the rest of the code works! It is usually best to solve one small problem at a time. – ewramner Jun 29 '18 at 12:11

0 Answers0