We're using Oracle db and it makes no difference, IMO.
Here it goes for functions and pkg call that returns a cursor.
For FUNCTION Call that returns one value only: ------------------------------------ <sql-query name="FN_GET_NBR" callable="false"> <query-param name="param" type="java.lang.String"/> <query-param name="param" type="java.lang.String"/> <query-param name="param" type="java.lang.Integer"/> <![CDATA[ select FN_GET_NBR(?,?,?) from dual ]]> </sql-query>
Code: ---------- Query namedQry = session.getNamedQuery("FN_GET_NBR"); namedQry.setString(0, strOne); namedQry.setString(1, strTwo); namedQry.setInteger(2, num); List searchResults = namedQry.list(); BigDecimal result = (BigDecimal) searchResults.get(0); // if not null then show the value System.out.println(result.intValue());
For Package Call that returns a cursor (multiple rows): ------------------------------------------------------- <sql-query name="PKG.RESULTS" callable="true"> <query-param name="param" type="java.lang.Long"/> <!-- Value 1 --> <query-param name="param" type="java.lang.Long"/> <!-- Value 2 --> <query-param name="param" type="java.lang.Integer"/> <!-- Value 3 --> {?= call PKG.RESULTS(?,?,?) } </sql-query>
Code: ---------- Query namedQuery = session.getNamedQuery("PKG.RESULTS"); namedQuery.setLong(0, arg1); namedQuery.setLong(1, arg2); namedQuery.setInteger(2, arg3); resultsList = namedQuery.list();
Iterator itemIter = resultsList.iterator(); List dtoList = new ArrayList(); yourClassDTO d = null;
Object[] result = null; while (iterator.hasNext()) { result = (Object[]) itemIter.next(); Long itemId = (result[0] != null) ? new Long(result[0].toString()) : null; Integer typeId = (result[1] != null) ? new Integer(result[1].toString()) : null; d = yourClassDTO(); d.setItemId(itemId); d.setTypId(typeId); dtoList.add(d); }
return dtoList;
You have to iterate through the resultsList and parse each row to get the values. For this, you need to know the number of columns and their type in rows returned to parse it correctly. You can instantiate your class [yourClassDTO], populate with the values in the object array above, build a list with your dto class and return it. That's pretty much it.
Hope this helps to get to the next step :)
|