Class BeanProcessor
- java.lang.Object
-
- org.apache.commons.dbutils.BeanProcessor
-
- Direct Known Subclasses:
GenerousBeanProcessor
public class BeanProcessor extends java.lang.ObjectBeanProcessormatches column names to bean property names and convertsResultSetcolumns into objects for those bean properties. Subclasses should override the methods in the processing chain to customize behavior.This class is thread-safe.
- Since:
- 1.1
- See Also:
BasicRowProcessor
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.List<ColumnHandler<?>>COLUMN_HANDLERSprivate java.util.Map<java.lang.String,java.lang.String>columnToPropertyOverridesResultSet column to bean property name overrides.private static java.util.Map<java.lang.Class<?>,java.lang.Object>PRIMITIVE_DEFAULTSSet a bean's primitive properties to these defaults when SQL NULL is returned.private static java.util.List<PropertyHandler>PROPERTY_HANDLERSprotected static intPROPERTY_NOT_FOUNDSpecial array value used bymapColumnsToPropertiesthat indicates there is no bean property that matches a column from aResultSet.
-
Constructor Summary
Constructors Constructor Description BeanProcessor()Constructor for BeanProcessor.BeanProcessor(java.util.Map<java.lang.String,java.lang.String> columnToPropertyOverrides)Constructor for BeanProcessor configured with column to property name overrides.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcallSetter(java.lang.Object target, java.beans.PropertyDescriptor prop, java.lang.Object value)Calls the setter method on the target object for the given property.private <T> TcreateBean(java.sql.ResultSet resultSet, java.lang.Class<T> type, java.beans.PropertyDescriptor[] props, int[] columnToProperty)Creates a new object and initializes its fields from the ResultSet.protected java.lang.reflect.MethodgetWriteMethod(java.lang.Object target, java.beans.PropertyDescriptor prop, java.lang.Object value)Get the write method to use when settingvalueto thetarget.private booleanisCompatibleType(java.lang.Object value, java.lang.Class<?> type)ResultSet.getObject() returns an Integer object for an INT column.protected int[]mapColumnsToProperties(java.sql.ResultSetMetaData rsmd, java.beans.PropertyDescriptor[] props)The positions in the returned array represent column numbers.private booleanmatchesPrimitive(java.lang.Class<?> targetType, java.lang.Class<?> valueType)Check whether a value is of the same primitive type astargetType.protected <T> TnewInstance(java.lang.Class<T> c)Factory method that returns a new instance of the given Class.<T> TpopulateBean(java.sql.ResultSet resultSet, T bean)Initializes the fields of the provided bean from the ResultSet.private <T> TpopulateBean(java.sql.ResultSet resultSet, T bean, java.beans.PropertyDescriptor[] props, int[] columnToProperty)This method populates a bean from the ResultSet based upon the underlying meta-data.protected java.lang.ObjectprocessColumn(java.sql.ResultSet resultSet, int index, java.lang.Class<?> propType)Convert aResultSetcolumn into an object.private java.beans.PropertyDescriptor[]propertyDescriptors(java.lang.Class<?> c)Returns a PropertyDescriptor[] for the given Class.<T> TtoBean(java.sql.ResultSet rs, java.lang.Class<? extends T> type)Convert aResultSetrow into a JavaBean.<T> java.util.List<T>toBeanList(java.sql.ResultSet resultSet, java.lang.Class<? extends T> type)Convert aResultSetinto aListof JavaBeans.
-
-
-
Field Detail
-
PROPERTY_NOT_FOUND
protected static final int PROPERTY_NOT_FOUND
Special array value used bymapColumnsToPropertiesthat indicates there is no bean property that matches a column from aResultSet.- See Also:
- Constant Field Values
-
PRIMITIVE_DEFAULTS
private static final java.util.Map<java.lang.Class<?>,java.lang.Object> PRIMITIVE_DEFAULTS
Set a bean's primitive properties to these defaults when SQL NULL is returned. These are the same as the defaults that ResultSet get* methods return in the event of a NULL column.
-
COLUMN_HANDLERS
private static final java.util.List<ColumnHandler<?>> COLUMN_HANDLERS
-
PROPERTY_HANDLERS
private static final java.util.List<PropertyHandler> PROPERTY_HANDLERS
-
columnToPropertyOverrides
private final java.util.Map<java.lang.String,java.lang.String> columnToPropertyOverrides
ResultSet column to bean property name overrides.
-
-
Constructor Detail
-
BeanProcessor
public BeanProcessor()
Constructor for BeanProcessor.
-
BeanProcessor
public BeanProcessor(java.util.Map<java.lang.String,java.lang.String> columnToPropertyOverrides)
Constructor for BeanProcessor configured with column to property name overrides.- Parameters:
columnToPropertyOverrides- ResultSet column to bean property name overrides- Since:
- 1.5
-
-
Method Detail
-
callSetter
private void callSetter(java.lang.Object target, java.beans.PropertyDescriptor prop, java.lang.Object value) throws java.sql.SQLExceptionCalls the setter method on the target object for the given property. If no setter method exists for the property, this method does nothing.- Parameters:
target- The object to set the property on.prop- The property to set.value- The value to pass into the setter.- Throws:
java.sql.SQLException- if an error occurs setting the property.
-
createBean
private <T> T createBean(java.sql.ResultSet resultSet, java.lang.Class<T> type, java.beans.PropertyDescriptor[] props, int[] columnToProperty) throws java.sql.SQLExceptionCreates a new object and initializes its fields from the ResultSet.- Type Parameters:
T- The type of bean to create- Parameters:
resultSet- The result set.type- The bean type (the return type of the object).props- The property descriptors.columnToProperty- The column indices in the result set.- Returns:
- An initialized object.
- Throws:
java.sql.SQLException- if a database error occurs.
-
getWriteMethod
protected java.lang.reflect.Method getWriteMethod(java.lang.Object target, java.beans.PropertyDescriptor prop, java.lang.Object value)Get the write method to use when settingvalueto thetarget.- Parameters:
target- Object where the write method will be called.prop- BeanUtils information.value- The value that will be passed to the write method.- Returns:
- The
Methodto call ontargetto writevalueornullif there is no suitable write method.
-
isCompatibleType
private boolean isCompatibleType(java.lang.Object value, java.lang.Class<?> type)ResultSet.getObject() returns an Integer object for an INT column. The setter method for the property might take an Integer or a primitive int. This method returns true if the value can be successfully passed into the setter method. Remember, Method.invoke() handles the unwrapping of Integer into an int.- Parameters:
value- The value to be passed into the setter method.type- The setter's parameter type (non-null)- Returns:
- boolean True if the value is compatible (null => true)
-
mapColumnsToProperties
protected int[] mapColumnsToProperties(java.sql.ResultSetMetaData rsmd, java.beans.PropertyDescriptor[] props) throws java.sql.SQLExceptionThe positions in the returned array represent column numbers. The values stored at each position represent the index in thePropertyDescriptor[]for the bean property that matches the column name. If no bean property was found for a column, the position is set toPROPERTY_NOT_FOUND.- Parameters:
rsmd- TheResultSetMetaDatacontaining column information.props- The bean property descriptors.- Returns:
- An int[] with column index to property index mappings. The 0th element is meaningless because JDBC column indexing starts at 1.
- Throws:
java.sql.SQLException- if a database access error occurs
-
matchesPrimitive
private boolean matchesPrimitive(java.lang.Class<?> targetType, java.lang.Class<?> valueType)Check whether a value is of the same primitive type astargetType.- Parameters:
targetType- The primitive type to target.valueType- The value to match to the primitive type.- Returns:
- Whether
valueTypecan be coerced (e.g. autoboxed) intotargetType.
-
newInstance
protected <T> T newInstance(java.lang.Class<T> c) throws java.sql.SQLExceptionFactory method that returns a new instance of the given Class. This is called at the start of the bean creation process and may be overridden to provide custom behavior like returning a cached bean instance.- Type Parameters:
T- The type of object to create- Parameters:
c- The Class to create an object from.- Returns:
- A newly created object of the Class.
- Throws:
java.sql.SQLException- if creation failed.
-
populateBean
public <T> T populateBean(java.sql.ResultSet resultSet, T bean) throws java.sql.SQLExceptionInitializes the fields of the provided bean from the ResultSet.- Type Parameters:
T- The type of bean- Parameters:
resultSet- The result set.bean- The bean to be populated.- Returns:
- An initialized object.
- Throws:
java.sql.SQLException- if a database error occurs.
-
populateBean
private <T> T populateBean(java.sql.ResultSet resultSet, T bean, java.beans.PropertyDescriptor[] props, int[] columnToProperty) throws java.sql.SQLExceptionThis method populates a bean from the ResultSet based upon the underlying meta-data.- Type Parameters:
T- The type of bean- Parameters:
resultSet- The result set.bean- The bean to be populated.props- The property descriptors.columnToProperty- The column indices in the result set.- Returns:
- An initialized object.
- Throws:
java.sql.SQLException- if a database error occurs.
-
processColumn
protected java.lang.Object processColumn(java.sql.ResultSet resultSet, int index, java.lang.Class<?> propType) throws java.sql.SQLExceptionConvert aResultSetcolumn into an object. Simple implementations could just callrs.getObject(index)while more complex implementations could perform type manipulation to match the column's type to the bean property type.This implementation calls the appropriate
ResultSetgetter method for the given property type to perform the type conversion. If the property type doesn't match one of the supportedResultSettypes,getObjectis called.- Parameters:
resultSet- TheResultSetcurrently being processed. It is positioned on a valid row before being passed into this method.index- The current column index being processed.propType- The bean property type that this column needs to be converted into.- Returns:
- The object from the
ResultSetat the given column index after optional type processing ornullif the column value was SQL NULL. - Throws:
java.sql.SQLException- if a database access error occurs
-
propertyDescriptors
private java.beans.PropertyDescriptor[] propertyDescriptors(java.lang.Class<?> c) throws java.sql.SQLExceptionReturns a PropertyDescriptor[] for the given Class.- Parameters:
c- The Class to retrieve PropertyDescriptors for.- Returns:
- A PropertyDescriptor[] describing the Class.
- Throws:
java.sql.SQLException- if introspection failed.
-
toBean
public <T> T toBean(java.sql.ResultSet rs, java.lang.Class<? extends T> type) throws java.sql.SQLExceptionConvert aResultSetrow into a JavaBean. This implementation uses reflection andBeanInfoclasses to match column names to bean property names. Properties are matched to columns based on several factors: <br/> <ol> <li> The class has a writable property with the same name as a column. The name comparison is case insensitive. </li> <li> The column type can be converted to the property's set method parameter type with a ResultSet.get* method. If the conversion fails (ie. the property was an int and the column was a Timestamp) an SQLException is thrown. </li> </ol> <p> Primitive bean properties are set to their defaults when SQL NULL is returned from theResultSet. Numeric fields are set to 0 and booleans are set to false. Object bean properties are set tonullwhen SQL NULL is returned. This is the same behavior as theResultSetget* methods. </p>- Type Parameters:
T- The type of bean to create- Parameters:
rs- ResultSet that supplies the bean datatype- Class from which to create the bean instance- Returns:
- the newly created bean
- Throws:
java.sql.SQLException- if a database access error occurs
-
toBeanList
public <T> java.util.List<T> toBeanList(java.sql.ResultSet resultSet, java.lang.Class<? extends T> type) throws java.sql.SQLExceptionConvert aResultSetinto aListof JavaBeans. This implementation uses reflection andBeanInfoclasses to match column names to bean property names. Properties are matched to columns based on several factors: <br/> <ol> <li> The class has a writable property with the same name as a column. The name comparison is case insensitive. </li> <li> The column type can be converted to the property's set method parameter type with a ResultSet.get* method. If the conversion fails (ie. the property was an int and the column was a Timestamp) an SQLException is thrown. </li> </ol>Primitive bean properties are set to their defaults when SQL NULL is returned from the
ResultSet. Numeric fields are set to 0 and booleans are set to false. Object bean properties are set tonullwhen SQL NULL is returned. This is the same behavior as theResultSetget* methods. </p>- Type Parameters:
T- The type of bean to create- Parameters:
resultSet- ResultSet that supplies the bean datatype- Class from which to create the bean instance- Returns:
- the newly created List of beans
- Throws:
java.sql.SQLException- if a database access error occurs
-
-