Class BeanProcessor
DB.updateModel(Entity, Map)
would allow to update the model with database meta data, but for
efficiency reasons Beans.updateModel(Entity, Map) us used.
Originally from the 1.8 version of apache's common-dbutils:
https://github.com/apache/commons-dbutils/blob/master/src/main/java/org/apache/commons/dbutils/BeanProcessor.java
BeanProcessor matches column names to bean property names and
converts ResultSet columns 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
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intSpecial array value used bymapColumnsToPropertiesthat indicates there is no bean property that matches a column from aResultSet. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected MethodgetWriteMethod(Object target, BeanField beanField, Object value) Get the write method to use when settingvalueto thetarget.protected int[]mapColumnsToProperties(ResultSetMetaData rsmd, BeanField[] beanFields) The positions in the returned array represent column numbers.protected int[]mapColumnsToPropertiesWithAllMethods(ResultSetMetaData rsmd, BeanField[] beanFields) The positions in the returned array represent column numbers.protected <T> TnewInstance(Class<T> c) Factory method that returns a new instance of the given Class.populateBean(ResultSet rs, Entity entity) Initializes the fields of the provided bean from the ResultSet.protected ObjectprocessColumn(ResultSet rs, int index, Class<?> propType) Convert aResultSetcolumn into an object.Convert aResultSetrow into a JavaBean.toBeanList(ResultSet rs, Class<?> type) Convert aResultSetinto aListof JavaBeans.
-
Field Details
-
PROPERTY_NOT_FOUND
protected static final int PROPERTY_NOT_FOUNDSpecial array value used bymapColumnsToPropertiesthat indicates there is no bean property that matches a column from aResultSet.- See Also:
-
-
Constructor Details
-
BeanProcessor
public BeanProcessor()Constructor for BeanProcessor.
-
-
Method Details
-
toBean
Convert 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>- Parameters:
rs- ResultSet that supplies the bean datatype- Class from which to create the bean instance- Returns:
- the newly created bean
- Throws:
SQLException- if a database access error occurs
-
toBeanList
Convert 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>- Parameters:
rs- ResultSet that supplies the bean datatype- Class from which to create the bean instance- Returns:
- the newly created List of beans
- Throws:
SQLException- if a database access error occurs
-
populateBean
Initializes the fields of the provided bean from the ResultSet.- Parameters:
rs- The result set.entity- The bean to be populated.- Returns:
- An initialized object.
- Throws:
SQLException- if a database error occurs.
-
getWriteMethod
Get the write method to use when settingvalueto thetarget.- Parameters:
target- Object where the write method will be called.beanField- Bean field information.value- The value that will be passed to the write method.- Returns:
- The
Methodto call ontargetto writevalueornullif there is no suitable write method.
-
newInstance
Factory 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:
SQLException- if creation failed.
-
mapColumnsToProperties
protected int[] mapColumnsToProperties(ResultSetMetaData rsmd, BeanField[] beanFields) throws SQLException The positions in the returned array represent column numbers. The values stored at each position represent the index in theBeanField[]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.beanFields- The bean fields.- Returns:
- An int[] with column index to property index mappings. The 0th element is meaningless because JDBC column indexing starts at 1.
- Throws:
SQLException- if a database access error occurs
-
mapColumnsToPropertiesWithAllMethods
protected int[] mapColumnsToPropertiesWithAllMethods(ResultSetMetaData rsmd, BeanField[] beanFields) throws SQLException The positions in the returned array represent column numbers. The values stored at each position represent the index in theBeanField[]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. This method tries not only to map properties by bean fields, it also tries to map properties by comparing property names to table column names (snake- to camel-case conversion) if no 'Column'-annotations are found!- Parameters:
rsmd- TheResultSetMetaDatacontaining column information.beanFields- The bean fields.- Returns:
- An int[] with column index to property index mappings. The 0th element is meaningless because JDBC column indexing starts at 1.
- Throws:
SQLException- if a database access error occurs
-
processColumn
Convert 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:
rs- 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:
SQLException- if a database access error occurs
-