You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by C....@ads.it on 2006/10/12 12:38:48 UTC

Dynamic packages

Hi all

I have to pass at runtime the package name to access its get_key_1(),
get_key_2(), ... methods

The aim is to get the feneration of the

   { ut_Tipo_Dato.get_key_2( ? )  }
   { ut_Voce.get_key_2( ? )  }

to retrieve data for stored package procedures such as

   procedure get_key_2
   ( p_tipo_dato_ID  out number(10)
   );


Well, where to begin?

By using the $$ syntax ?

   $packageId$.get_key_2( ? )

Using both $substitution$ and #parameters# along with <iterate> (Dynamic
tables thread)?

Using Dynamic Mapped Statements? (pag. 46, iBATIS-SqlMaps-2_en.pdf)?

Unfortunately I have not found yet a simple and complete example provided
with both the  XML config specification and the mini Java snippet code that
exerts it.

I guess that we have to use any Map to provide parameters, but there is, as
well, che OUT parameter for the procedure itself.
How to combine the parameterMap, the JavaBean class (devoted to the
procedure parameters) and the $sobstitution$ parameters?

A complete example would be of great help to start!


Thanks in advance

Cesare

Re: Dynamic packages (works! :)

Posted by C....@ads.it.


Hi Jeff.
Thanks for the help.

It works fine! :)
To help any other beginner that will face the issue go dyamic spcification
of a package name (or othe parts of the SQL statement to be prepared)
here follow the code snippets

SQL Map

      <procedure id="dynamic_package_id" parameterClass="java.util.Map">
        { call $packageName$.get_key_2_out_basic(
#outParam,javaType=java.lang.Integer,mode=OUT,jdbcType=INTEGER# ) }
      </procedure>

Java

      HashMap< String, Object > lHashMap = new HashMap< String, Object >();
      lHashMap.put("packageName", "ut_Foo");
       try
       {
           lSqlMapClient.update( "dynamic_package_id", lHashMap );
       }
       catch (SQLException pException)
       {
            pException.printStackTrace();
            throw pException;
       }
       Integer lInteger = (Integer) lHashMap.get("outParam");
       assertEquals( 5, lInteger.intValue() );


PL/SQL
(ut parts not relevant have been omitted)
specification

   create or replace package ut_foo is

     procedure get_key_2_out_basic( p_PK out number );
     pragma restrict_references( get_key_2_out_basic, WNDS );

   end ut_foo;

body

   create or replace package body ut_foo is

     procedure ut_setup
     is
     begin
       null;
     end;

     procedure ut_teardown
     is
     begin
       null;
     end;

     procedure get_key_2_out_basic( p_PK out number )
     is
     begin
       p_PK := 5;
     end;

   begin
     null;
   end ut_foo;

I hope it will help for other newcomers.

ciao

Re: Dynamic packages

Posted by Jeff Butler <je...@gmail.com>.
To accomplish this you'll need to skip the parameter map and use the
advanced inline parameter syntax.  Here's an example:

*SQL Map*

<procedure id="dynamic_package_id" parameterClass="map">
  { call $packageName$.get_key_2_out_basic( #outParam,javaType=
java.lang.Integer,mode=OUT,jdbcType=INTEGER# ) }
</procedure>


*Java*



Map parms = new HashMap();
parms.put("packageName", "ut_Tipo_dato");

sqlMapClient.update("dynamic_package_id", parms);
Integer result = (Integer) parms.get("outParam");

Jeff Butler



On 10/12/06, C.Zecca@ads.it <C....@ads.it> wrote:
>
>  larry.meadors@gmail.com scritti il 12/10/2006 13:36:36
>
> > Put the bean in the Map, call it "bean" and refer to it's properties
> > in the mapped statement (or parameter map) with a "bean." prefix:
> > "bean.someProperty".
> >
> > There are not many examples of the Java code because it's pretty much
> > all the same:
> >
> > sqlMapClient.almostAnyMethod("namespace.statement", parameterObject);
>
> Thanks Larry.
> I tried to interpret your suggestion
>
> Firstly I tried this
>
> <parameterMap id="paraMap" class="it.finmatica.gpj.aa.frontebd.ChiaveBean
> ">
> <parameter value="outParam" property="id" jdbcType="INTEGER"
> javaType="int" mode="OUT"/>
> </parameterMap>
>
> <procedure id="dynamic_package_id" parameterMap="paraMap">
> { call $packageName$.get_key_2_out_basic( ? ) }
> </procedure>
>
> (I read the "value" techinque for Maps in the list somewhere but,
> unfortunately I do not find it anymore)
> with this Java code
>
> HashMap< String, Object > lHashMap = new HashMap< String, Object >();
>     ChiaveBean lChiaveBean = new ChiaveBean( 1234 );
> [*]   lHashMap.put( "outParam", lChiaveBean );
>     lHashMap.put( "packageName", new String( "ut_Tipo_dato" ) );
>       try
>         {
>         lSqlMapClient.update( "dynamic_package_id", lHashMap );
>
> (the doubt here was "*how can I pass the parameter for $packageName$?*",
> so I tried by adding a second item to that map with the "packageName" key)
> and the diagnostic)
> The diagnostic complained about the value for the "parameter" element
> type.
>
>    [...] Error parsing XML.  Cause: *org.xml.sax.SAXParseException*:
>    Attribute "value" must be declared for element type "parameter".
>
>
>
> I've removed the "value" element type (infact, the syntax for
> parameterMaps does not accept it) an I got
>
>    --- The error occurred in
>    it/finmatica/gpj/aa/frontebd/DynamicTableTest.xml.
>    --- The error occurred while preparing the mapped statement for
>    execution.
>    --- Check the dynamic_package_id.
>    --- Cause: *java.sql.SQLException*: Invalid parameter object type.
>     Expected 'it.finmatica.gpj.aa.frontebd.ChiaveBean' but found '
>    java.util.HashMap'.
>
> iBatis complains that wants a ChiaveBean
> Well, if I pass a ChiaveBean, how can I provide the $packageName$?
> Anyway, let's try
>
>    --- The error occurred in
>    it/finmatica/gpj/aa/frontebd/DynamicTableTest.xml.
>    --- The error occurred while preparing the mapped statement for
>    execution.
>    --- Check the dynamic_package_id.
>    --- Check the SQL statement.
>    --- Cause: *com.ibatis.common.beans.ProbeException*: There is no
>    READABLE property named 'packageName' in class '
>    it.finmatica.gpj.aa.frontebd.ChiaveBean'
>
>
> iBatis searches the ChiaveBean for a packageName property.
> I'm beginning to think that I should write a suitable JavaBean to provide
> a property packageName, as well... :( Right?
> We have some JavaBeans classes whose structure reflect the data on the DB
> and we would prefer not to pollute them with data needed to dinamically
> generate the SQL statements (we might extend the JavaBean classes but it
> would be, anyway, an extrema ratio)
>
>
> Is anywayt to keep distinct the parameters for the procedure (parameterMap
> + a suitable Java Bean)
> and the parameters to be used to make the SQL statement, such as
> $packageName$?
> Perhaps using a HashMap and the actual parameters passed with their name
> as depicted above ([*])?
>
>
> ciao
> Cesare
>
>

Re: Dynamic packages

Posted by C....@ads.it.
larry.meadors@gmail.com scritti il 12/10/2006 13:36:36

> Put the bean in the Map, call it "bean" and refer to it's properties
> in the mapped statement (or parameter map) with a "bean." prefix:
> "bean.someProperty".
>
> There are not many examples of the Java code because it's pretty much
> all the same:
>
> sqlMapClient.almostAnyMethod("namespace.statement", parameterObject);

Thanks Larry.
I tried to interpret your suggestion

Firstly I tried this

      <parameterMap id="paraMap"
class="it.finmatica.gpj.aa.frontebd.ChiaveBean">
            <parameter value="outParam" property="id" jdbcType="INTEGER"
javaType="int" mode="OUT"/>
      </parameterMap>

      <procedure id="dynamic_package_id" parameterMap="paraMap">
            { call $packageName$.get_key_2_out_basic( ? ) }
      </procedure>

(I read the "value" techinque for Maps in the list somewhere but,
unfortunately I do not find it anymore)
with this Java code

      HashMap< String, Object > lHashMap = new HashMap< String, Object >();
      ChiaveBean lChiaveBean = new ChiaveBean( 1234 );
[*]   lHashMap.put( "outParam", lChiaveBean );
      lHashMap.put( "packageName", new String( "ut_Tipo_dato" ) );
      try
        {
            lSqlMapClient.update( "dynamic_package_id", lHashMap );

(the doubt here was "how can I pass the parameter for $packageName$?", so I
tried by adding a second item to that map with the "packageName" key) and
the diagnostic)
The diagnostic complained about the value for the "parameter" element type.

   [...] Error parsing XML.  Cause: org.xml.sax.SAXParseException:
   Attribute "value" must be declared for element type "parameter".


I've removed the "value" element type (infact, the syntax for parameterMaps
does not accept it)  an I got

   --- The error occurred in
   it/finmatica/gpj/aa/frontebd/DynamicTableTest.xml.
   --- The error occurred while preparing the mapped statement for
   execution.
   --- Check the dynamic_package_id.
   --- Cause: java.sql.SQLException: Invalid parameter object type.
   Expected 'it.finmatica.gpj.aa.frontebd.ChiaveBean' but found
   'java.util.HashMap'.

iBatis complains that wants a ChiaveBean
Well, if I pass a ChiaveBean, how can I provide the $packageName$?
Anyway, let's try

   --- The error occurred in
   it/finmatica/gpj/aa/frontebd/DynamicTableTest.xml.
   --- The error occurred while preparing the mapped statement for
   execution.
   --- Check the dynamic_package_id.
   --- Check the SQL statement.
   --- Cause: com.ibatis.common.beans.ProbeException: There is no READABLE
   property named 'packageName' in class
   'it.finmatica.gpj.aa.frontebd.ChiaveBean'

iBatis searches the ChiaveBean for a packageName property.
I'm beginning to think that I should write a suitable JavaBean to provide a
property packageName, as well... :( Right?
We have some JavaBeans classes whose structure reflect the data on the DB
and we would prefer not to pollute them with data needed to dinamically
generate the SQL statements (we might extend the JavaBean classes but it
would be, anyway, an extrema ratio)


Is anywayt to keep distinct the parameters for the procedure (parameterMap
+ a suitable Java Bean)
and the parameters to be used to make the SQL statement, such as
$packageName$?
Perhaps using a HashMap and the actual parameters passed with their name as
depicted above ([*])?


ciao
Cesare

Re: Dynamic packages

Posted by Larry Meadors <lm...@apache.org>.
Put the bean in the Map, call it "bean" and refer to it's properties
in the mapped statement (or parameter map) with a "bean." prefix:
"bean.someProperty".

There are not many examples of the Java code because it's pretty much
all the same:

sqlMapClient.almostAnyMethod("namespace.statement", parameterObject);

Larry

On 10/12/06, C.Zecca@ads.it <C....@ads.it> wrote:
>
>
> Hi all
>
>  I have to pass at runtime the package name to access its get_key_1(),
> get_key_2(), ... methods
>
>  The aim is to get the feneration of the
>
> { ut_Tipo_Dato.get_key_2( ? )  }
>  { ut_Voce.get_key_2( ? )  }
>  to retrieve data for stored package procedures such as
>
>  procedure get_key_2
>  ( p_tipo_dato_ID out number(10)
>  );
>
>
>  Well, where to begin?
>
>  By using the $$ syntax ?
>
> $packageId$.get_key_2( ? )
>  Using both $substitution$ and #parameters# along with <iterate> (Dynamic
> tables thread)?
>
>  Using Dynamic Mapped Statements? (pag. 46, iBATIS-SqlMaps-2_en.pdf)?
>
>  Unfortunately I have not found yet a simple and complete example provided
> with both the XML config specification and the mini Java snippet code that
> exerts it.
>
>  I guess that we have to use any Map to provide parameters, but there is, as
> well, che OUT parameter for the procedure itself.
>  How to combine the parameterMap, the JavaBean class (devoted to the
> procedure parameters) and the $sobstitution$ parameters?
>
>  A complete example would be of great help to start!
>
>
>  Thanks in advance
>
>  Cesare
>
>