|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.commons.dbutils.BeanProcessor
public class BeanProcessor
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.
BasicRowProcessor
Field Summary | |
---|---|
private static java.util.Map |
primitiveDefaults
Set a bean's primitive properties to these defaults when SQL NULL is returned. |
protected static int |
PROPERTY_NOT_FOUND
Special array value used by mapColumnsToProperties that
indicates there is no bean property that matches a column from a
ResultSet . |
Constructor Summary | |
---|---|
BeanProcessor()
Constructor for BeanProcessor. |
Method Summary | |
---|---|
private void |
callSetter(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 java.lang.Object |
createBean(java.sql.ResultSet rs,
java.lang.Class type,
java.beans.PropertyDescriptor[] props,
int[] columnToProperty)
Creates a new object and initializes its fields from the ResultSet. |
private boolean |
isCompatibleType(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. |
protected java.lang.Object |
newInstance(java.lang.Class c)
Factory method that returns a new instance of the given Class. |
protected java.lang.Object |
processColumn(java.sql.ResultSet rs,
int index,
java.lang.Class propType)
Convert a ResultSet column into an object. |
private java.beans.PropertyDescriptor[] |
propertyDescriptors(java.lang.Class c)
Returns a PropertyDescriptor[] for the given Class. |
java.lang.Object |
toBean(java.sql.ResultSet rs,
java.lang.Class type)
Convert a ResultSet row into a JavaBean. |
java.util.List |
toBeanList(java.sql.ResultSet rs,
java.lang.Class type)
Convert a ResultSet into a List of JavaBeans. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final int PROPERTY_NOT_FOUND
mapColumnsToProperties
that
indicates there is no bean property that matches a column from a
ResultSet
.
private static final java.util.Map primitiveDefaults
Constructor Detail |
---|
public BeanProcessor()
Method Detail |
---|
public java.lang.Object toBean(java.sql.ResultSet rs, java.lang.Class type) throws java.sql.SQLException
ResultSet
row into a JavaBean. This
implementation uses reflection and BeanInfo
classes to
match column names to bean property names. Properties are matched to
columns based on several factors:
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 to
null
when SQL NULL is returned. This is the same behavior
as the ResultSet
get* methods.
rs
- ResultSet that supplies the bean datatype
- Class from which to create the bean instance
java.sql.SQLException
- if a database access error occurspublic java.util.List toBeanList(java.sql.ResultSet rs, java.lang.Class type) throws java.sql.SQLException
ResultSet
into a List
of JavaBeans.
This implementation uses reflection and BeanInfo
classes to
match column names to bean property names. Properties are matched to
columns based on several factors:
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 to
null
when SQL NULL is returned. This is the same behavior
as the ResultSet
get* methods.
rs
- ResultSet that supplies the bean datatype
- Class from which to create the bean instance
java.sql.SQLException
- if a database access error occursprivate java.lang.Object createBean(java.sql.ResultSet rs, java.lang.Class type, java.beans.PropertyDescriptor[] props, int[] columnToProperty) throws java.sql.SQLException
rs
- 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.
java.sql.SQLException
- if a database error occurs.private void callSetter(java.lang.Object target, java.beans.PropertyDescriptor prop, java.lang.Object value) throws java.sql.SQLException
target
- The object to set the property on.prop
- The property to set.value
- The value to pass into the setter.
java.sql.SQLException
- if an error occurs setting the property.private boolean isCompatibleType(java.lang.Object value, java.lang.Class type)
value
- The value to be passed into the setter method.type
- The setter's parameter type.
protected java.lang.Object newInstance(java.lang.Class c) throws java.sql.SQLException
c
- The Class to create an object from.
java.sql.SQLException
- if creation failed.private java.beans.PropertyDescriptor[] propertyDescriptors(java.lang.Class c) throws java.sql.SQLException
c
- The Class to retrieve PropertyDescriptors for.
java.sql.SQLException
- if introspection failed.protected int[] mapColumnsToProperties(java.sql.ResultSetMetaData rsmd, java.beans.PropertyDescriptor[] props) throws java.sql.SQLException
PropertyDescriptor[]
for the bean property that matches
the column name. If no bean property was found for a column, the
position is set to PROPERTY_NOT_FOUND
.
rsmd
- The ResultSetMetaData
containing column
information.props
- The bean property descriptors.
java.sql.SQLException
- if a database access error occursprotected java.lang.Object processColumn(java.sql.ResultSet rs, int index, java.lang.Class propType) throws java.sql.SQLException
ResultSet
column into an object. Simple
implementations could just call rs.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 ResultSet
getter
method for the given property type to perform the type conversion. If
the property type doesn't match one of the supported
ResultSet
types, getObject
is called.
rs
- The ResultSet
currently 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.
ResultSet
at the given column
index after optional type processing or null
if the column
value was SQL NULL.
java.sql.SQLException
- if a database access error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |