Ted,
        Wow!

        I have to thank you for saving me from typing hundreds of lines of code
(well, I already wrote them -- now i'm gladly going to rip them out!) Using
BeanUtils to set properties of a bean from the column name of a ResultSet is
really smart. I've used your code fragment and it works great. However, I
noticed the "TODO:" says to let native types through. I would like to help
out on this part -- since I have a Timestamp datatype in my beans.

        Now, are there any plans to make BeanUtils support setting a Timestamp
method? How would I go about writing an extension to BeanUtils to support
different datatypes?

        I guess if I can't figure out how to set Timestamp datatypes in my beans,
I'll just change it to a string.

Mindaugas Idzelis



> -----Original Message-----
> From: Ted Husted [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, August 28, 2001 9:46 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Performance, Reflection, and Object Creation vs. Cacheing
>

<snip>

>
> Meanwhile, personally, I've started to build calls to
> BeanUtils.populate()
> into my own data transfer utilities. This lets me put a call deep inside
> the resource layer that neatly turns an arbitrary ResultSet into a
> collection of arbitrary beans. It just matches the rset columns with the
> jbean properties.
>
> // Transfer ResultSet to Collection of target beans **
> if (resultSet!=null) {
>       collection = ResultSetUtils.getCollection(
>               target,resultSet);
> }
>
> Where ResultSetUtils does this
>
>     public static void populate(Object bean,
>                                 ResultSet resultSet)
>         throws SQLException {
>         // Build a list of relevant column properties from this
> resultSet
>         HashMap properties = new HashMap();
>         // Acquire resultSet MetaData
>         ResultSetMetaData metaData = resultSet.getMetaData();
>         int cols = metaData.getColumnCount();
>         // Scroll to next record and pump into hashmap
>         if (resultSet.next()) for (int i=1; i<=cols ; i++) {
>             // :TODO: Let native types through
>             /*
>             int type = metaData.getType(i);
>             if ...
>             properties.put(metaData.getColumnName(i),
>                 resultSet.getObject(i));
>             else
>             */
>             properties.put(metaData.getColumnName(i),
>                 resultSet.getString(i));
>         }
>         // Set the corresponding properties of our bean
>         try {
>             BeanUtils.populate(bean, properties);
>         } catch (Exception e) {
>             throw new SQLException("BeanUtils.populate threw " +
> e.toString());
>         }
>     }
>
> This can save hundreds of lines of code that would have otherwise have
> been
> needed to write custom transfer utilities. (Been there, did that, not
> fun.)
>
> Of course, it works with more than just ResultSets. I wrote a similar
> set
> of utilities last week that turned a Lucene Hits list into a collection
> of beans. Sweet ;-)
>

<snip>



Reply via email to