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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]