You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Jorge Morales Pou <jm...@tid.es> on 2003/12/29 14:59:06 UTC

[DbUtils] Using BeanUtils converters

Hi,
  IŽm using the DbUtils component in a web application, and IŽm obtaining
  datatypes from the database which are not converted to the proper DTO data
  type. For instance, IŽm getting a Short from a table in a database which I
  have mapped to an Integer in my DTO. This conversion is not happening, since
  isCompatibleType in BasicRowProcessor is returning false.
  I have changed the implementation of the callSetter method, in the same class,
  so it uses ConvertUtils from the BeanUtils package.
  It works great with basic data types conversion, and if you want an explicit
  data type conversion, you just have to create a custom Converter, which IŽve
  been doing all over my app.
  I know this introduces a dependency on the BeanUtils package, but since I use
  it for other parts of my app, itŽs no problem.

    private void callSetter(
        Object target,
        PropertyDescriptor prop,
        Object value)
        throws SQLException {

        Method setter = prop.getWriteMethod();

        if (setter == null) {
            return;
        }

        Class[] params = setter.getParameterTypes();
        try {
            // Don't call setter if the value object isn't the right type 
            if (this.isCompatibleType(value, params[0])) {
                setter.invoke(target, new Object[] { value });
            }else{
                Converter converter = ConvertUtils.lookup(params[0]);
                // If there is a converter for the required data type, invoke
                // the setter method
                if (converter != null)
                    setter.invoke(target, new Object[] { converter.convert(params[0], value) });
            }

        } catch (IllegalArgumentException e) {
            throw new SQLException(
                "Cannot set " + prop.getName() + ": " + e.getMessage());

        } catch (IllegalAccessException e) {
            throw new SQLException(
                "Cannot set " + prop.getName() + ": " + e.getMessage());

        } catch (InvocationTargetException e) {
            throw new SQLException(
                "Cannot set " + prop.getName() + ": " + e.getMessage());
        }
    }


    I hope this helps anybody, and probably can be an enhacement to version 1.1
    
Happy holidays and happy new year from Spain,
-----------------------
"He pasado una noche estupenda.
 Pero no ha sido esta."
 GROUCHO MARX
-----------------------
Jorge Morales Pou
jmp07_ARROBA_tid.es
Telefónica I+D, Madrid.


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] Using BeanUtils converters

Posted by David Graham <gr...@yahoo.com>.
--- Jorge Morales Pou <jm...@tid.es> wrote:
> Hi David,
>   I have implemented my CustomRowProcessor, and used it in a static
> variable, as
>   you pointed, and everything works the same, that is, fine.
> 
>   I have finally seen that with this strategy no dependency on the
> BeaUtils
>   package is made for the DBUtils package, and agree with you that it�s
> is a
>   better apreoach, though not easy to see for a rokie like me.
> 
>   Thanks for all the help, and I hope to see some new work on Mapper,
> which I
>   think is a great utility in conjunction with DBUtils.

Feel free to discuss Mapper issues on commons-dev (for development) and
commons-user (for usage questions).  Most of the good stuff from Mapper
has made its way into other packages like DbUtils :-).

David

> 
>   Happy new year.
> -----------------------
> "A quien va usted a creer,
>  �A mi, o a sus propios ojos?"
>  GROUCHO MARX
> -----------------------
> Jorge Morales Pou
> Telef�nica I+D, Madrid.
> SPAIN
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] Using BeanUtils converters

Posted by Jorge Morales Pou <jm...@tid.es>.
Hi David,
  I have implemented my CustomRowProcessor, and used it in a static variable, as
  you pointed, and everything works the same, that is, fine.

  I have finally seen that with this strategy no dependency on the BeaUtils
  package is made for the DBUtils package, and agree with you that itŽs is a
  better apreoach, though not easy to see for a rokie like me.

  Thanks for all the help, and I hope to see some new work on Mapper, which I
  think is a great utility in conjunction with DBUtils.

  Happy new year.
-----------------------
"A quien va usted a creer,
 ¿A mi, o a sus propios ojos?"
 GROUCHO MARX
-----------------------
Jorge Morales Pou
Telefónica I+D, Madrid.
SPAIN


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] Using BeanUtils converters

Posted by David Graham <gr...@yahoo.com>.
--- Jorge Morales Pou <jm...@tid.es> wrote:
> Hi David,
>   From the reply I had from you, I dived into the latest snapshot code
> and tried
>   to get the picture of what you said.
>   As you say I would only have to do:
> 
>   List mylist = (List)
> run.query((String)queries.get(IQueries.ALL_EVENTS),
>                                  new BeanListHandler(Evento.class, new
>                                  BasicRowProcessor( new
> CustomColumnProcessor())));
> 
>   And implement my CustomColumnProcessor where I should convert my row
> from the
>   recordset into my bean, probably with BeanUtils.
> 
>   �Is this right?

Yes that is what I'm suggesting although I would never create new
BasicRowProcessor and CustomColumnProcessor instances for each query.  I
would cache them in a static variable and reuse them but the above code
works as a basic example.

> 
>   If I change BasicRowProcessor or make a custom RowProcessor, with the
> changes
>   I have made, it only introduces a dependency on other package (now
> Commons
>   Converters), and anyone can use it as long as they implement custom
> converters
>   for their particular type conversions. Wherever they choose to use
> custom
>   converters, they just have to register them, with something simolar
> to:
> 
>    ConvertUtils.register(new FechaConverter(),Fecha.class);
> 
>   before they make a query with such a data type.
> 
>   Not really sure which of the two alternatives would make code grow
> bigger as
>   the application grows.

The RowProcessor and ColumnProcessor interfaces are designed to allow you
to use whatever library you like behind the scenes for bean property
mapping and population while also allowing the core DbUtils code to remain
dependency free and ship as only one jar file.

David

> 
>   Thanks for your response, and sorry for my english.
> 
> 
> > Rather than add the dependency we added the ColumnProcessor interface.
> > See the process() method javadocs here:
> 
> >
>
http://jakarta.apache.org/commons/dbutils/apidocs/org/apache/commons/dbutils/ColumnProcessor.html
> 
> > You can implement this interface and use BeanUtils without adding a
> > dependency to DbUtils and without changing BasicRowProcessor.
> > ColumnProcessor will be included with 1.1 so let us know how it works
> for
> > you or if it could be improved before release.
> 
> > David
> 
>   
> 
> Saludos,
> -----------------------
> "Recordad que estamos luchando por el honor de esa mujer,
>  lo que probablemente es mas de lo que ella hizo jamas."
>  GROUCHO MARX
> -----------------------
> Jorge Morales Pou
> Telef�nica I+D, Madrid.
> SPAIN
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] Using BeanUtils converters

Posted by Jorge Morales Pou <jm...@tid.es>.
Hi David,
  From the reply I had from you, I dived into the latest snapshot code and tried
  to get the picture of what you said.
  As you say I would only have to do:

  List mylist = (List) run.query((String)queries.get(IQueries.ALL_EVENTS),
                                 new BeanListHandler(Evento.class, new
                                 BasicRowProcessor( new CustomColumnProcessor())));

  And implement my CustomColumnProcessor where I should convert my row from the
  recordset into my bean, probably with BeanUtils.

  ¿Is this right?

  If I change BasicRowProcessor or make a custom RowProcessor, with the changes
  I have made, it only introduces a dependency on other package (now Commons
  Converters), and anyone can use it as long as they implement custom converters
  for their particular type conversions. Wherever they choose to use custom
  converters, they just have to register them, with something simolar to:

   ConvertUtils.register(new FechaConverter(),Fecha.class);

  before they make a query with such a data type.

  Not really sure which of the two alternatives would make code grow bigger as
  the application grows.

  Thanks for your response, and sorry for my english.


> Rather than add the dependency we added the ColumnProcessor interface.
> See the process() method javadocs here:

> http://jakarta.apache.org/commons/dbutils/apidocs/org/apache/commons/dbutils/ColumnProcessor.html

> You can implement this interface and use BeanUtils without adding a
> dependency to DbUtils and without changing BasicRowProcessor.
> ColumnProcessor will be included with 1.1 so let us know how it works for
> you or if it could be improved before release.

> David

  

Saludos,
-----------------------
"Recordad que estamos luchando por el honor de esa mujer,
 lo que probablemente es mas de lo que ella hizo jamas."
 GROUCHO MARX
-----------------------
Jorge Morales Pou
Telefónica I+D, Madrid.
SPAIN


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] Using BeanUtils converters

Posted by Henri Yandell <ba...@generationjava.com>.
I've pulled the converters out to the Commons Convert library in the
sandbox by the way, so any dependency you might want to add doesn't have
to link to all of beanutils.

I'm using it in a couple of my own libraries to see how it feels etc.

Hen

On Mon, 29 Dec 2003, David Graham wrote:

> Rather than add the dependency we added the ColumnProcessor interface.
> See the process() method javadocs here:
>
> http://jakarta.apache.org/commons/dbutils/apidocs/org/apache/commons/dbutils/ColumnProcessor.html#process(java.sql.ResultSet,%20int,%20java.lang.Class)
>
> You can implement this interface and use BeanUtils without adding a
> dependency to DbUtils and without changing BasicRowProcessor.
> ColumnProcessor will be included with 1.1 so let us know how it works for
> you or if it could be improved before release.
>
> David
>
>
> --- Jorge Morales Pou <jm...@tid.es> wrote:
> > Hi,
> >   I�m using the DbUtils component in a web application, and I�m
> > obtaining
> >   datatypes from the database which are not converted to the proper DTO
> > data
> >   type. For instance, I�m getting a Short from a table in a database
> > which I
> >   have mapped to an Integer in my DTO. This conversion is not happening,
> > since
> >   isCompatibleType in BasicRowProcessor is returning false.
> >   I have changed the implementation of the callSetter method, in the
> > same class,
> >   so it uses ConvertUtils from the BeanUtils package.
> >   It works great with basic data types conversion, and if you want an
> > explicit
> >   data type conversion, you just have to create a custom Converter,
> > which I�ve
> >   been doing all over my app.
> >   I know this introduces a dependency on the BeanUtils package, but
> > since I use
> >   it for other parts of my app, it�s no problem.
> >
> >     private void callSetter(
> >         Object target,
> >         PropertyDescriptor prop,
> >         Object value)
> >         throws SQLException {
> >
> >         Method setter = prop.getWriteMethod();
> >
> >         if (setter == null) {
> >             return;
> >         }
> >
> >         Class[] params = setter.getParameterTypes();
> >         try {
> >             // Don't call setter if the value object isn't the right
> > type
> >             if (this.isCompatibleType(value, params[0])) {
> >                 setter.invoke(target, new Object[] { value });
> >             }else{
> >                 Converter converter = ConvertUtils.lookup(params[0]);
> >                 // If there is a converter for the required data type,
> > invoke
> >                 // the setter method
> >                 if (converter != null)
> >                     setter.invoke(target, new Object[] {
> > converter.convert(params[0], value) });
> >             }
> >
> >         } catch (IllegalArgumentException e) {
> >             throw new SQLException(
> >                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> >
> >         } catch (IllegalAccessException e) {
> >             throw new SQLException(
> >                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> >
> >         } catch (InvocationTargetException e) {
> >             throw new SQLException(
> >                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> >         }
> >     }
> >
> >
> >     I hope this helps anybody, and probably can be an enhacement to
> > version 1.1
> >
> > Happy holidays and happy new year from Spain,
> > -----------------------
> > "He pasado una noche estupenda.
> >  Pero no ha sido esta."
> >  GROUCHO MARX
> > -----------------------
> > Jorge Morales Pou
> > jmp07_ARROBA_tid.es
> > Telef�nica I+D, Madrid.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
>
>
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [DbUtils] Using BeanUtils converters

Posted by David Graham <gr...@yahoo.com>.
Rather than add the dependency we added the ColumnProcessor interface. 
See the process() method javadocs here:

http://jakarta.apache.org/commons/dbutils/apidocs/org/apache/commons/dbutils/ColumnProcessor.html#process(java.sql.ResultSet,%20int,%20java.lang.Class)

You can implement this interface and use BeanUtils without adding a
dependency to DbUtils and without changing BasicRowProcessor. 
ColumnProcessor will be included with 1.1 so let us know how it works for
you or if it could be improved before release.

David


--- Jorge Morales Pou <jm...@tid.es> wrote:
> Hi,
>   I�m using the DbUtils component in a web application, and I�m
> obtaining
>   datatypes from the database which are not converted to the proper DTO
> data
>   type. For instance, I�m getting a Short from a table in a database
> which I
>   have mapped to an Integer in my DTO. This conversion is not happening,
> since
>   isCompatibleType in BasicRowProcessor is returning false.
>   I have changed the implementation of the callSetter method, in the
> same class,
>   so it uses ConvertUtils from the BeanUtils package.
>   It works great with basic data types conversion, and if you want an
> explicit
>   data type conversion, you just have to create a custom Converter,
> which I�ve
>   been doing all over my app.
>   I know this introduces a dependency on the BeanUtils package, but
> since I use
>   it for other parts of my app, it�s no problem.
> 
>     private void callSetter(
>         Object target,
>         PropertyDescriptor prop,
>         Object value)
>         throws SQLException {
> 
>         Method setter = prop.getWriteMethod();
> 
>         if (setter == null) {
>             return;
>         }
> 
>         Class[] params = setter.getParameterTypes();
>         try {
>             // Don't call setter if the value object isn't the right
> type 
>             if (this.isCompatibleType(value, params[0])) {
>                 setter.invoke(target, new Object[] { value });
>             }else{
>                 Converter converter = ConvertUtils.lookup(params[0]);
>                 // If there is a converter for the required data type,
> invoke
>                 // the setter method
>                 if (converter != null)
>                     setter.invoke(target, new Object[] {
> converter.convert(params[0], value) });
>             }
> 
>         } catch (IllegalArgumentException e) {
>             throw new SQLException(
>                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> 
>         } catch (IllegalAccessException e) {
>             throw new SQLException(
>                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> 
>         } catch (InvocationTargetException e) {
>             throw new SQLException(
>                 "Cannot set " + prop.getName() + ": " + e.getMessage());
>         }
>     }
> 
> 
>     I hope this helps anybody, and probably can be an enhacement to
> version 1.1
>     
> Happy holidays and happy new year from Spain,
> -----------------------
> "He pasado una noche estupenda.
>  Pero no ha sido esta."
>  GROUCHO MARX
> -----------------------
> Jorge Morales Pou
> jmp07_ARROBA_tid.es
> Telef�nica I+D, Madrid.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org