net.sf.javaprinciples.persistence.db.jdbc.support.support
Class ConversionServiceAwareBeanPropertyRowMapper<T>

java.lang.Object
  extended by org.springframework.jdbc.core.BeanPropertyRowMapper<T>
      extended by org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper<T>
          extended by net.sf.javaprinciples.persistence.db.jdbc.support.support.ConversionServiceAwareBeanPropertyRowMapper<T>
Type Parameters:
T - The type of the result bean returned for each row.
All Implemented Interfaces:
org.springframework.jdbc.core.RowMapper<T>, org.springframework.jdbc.core.simple.ParameterizedRowMapper<T>

public class ConversionServiceAwareBeanPropertyRowMapper<T>
extends org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper<T>

An extension of the Spring JDBC ParameterizedBeanPropertyRowMapper providing additional application-specific extraction and conversion of SQL query results into Java object instances. Specifically, this class provides two useful enhancements: 1. Configures the conversionService property of BeanWrapper instances prior to their use in mapping columns from the ResultSet into Bean properties of result objects. This allows custom, specific type conversions to be configured for certain properties. One example of this is to take the (XML) contents of a CLOB and, using JAXB, unmarshall the XML into the correct object prior to setting it on the result bean. Additional conversions can be added as required via configuration. 2. Allows the setting of nested bean properties to an arbitrary depth based on the column label / name returned by a query. This allows for the population of an object graph directly from a query that may contain a set of complicated joins, allowing the database to be used for its strengths. An example of this is to find the latest version of a set of rows associated with a given entity via a foreign key relationship without having to fetch all of the rows and then iterate through them from within java. Such nested properties use the following syntax: propertyName_subPropertyName_subPropertyName and so on. If a query such as this one is executed: select p.value as propertyName_subPropertyName from table aTable p Then this mapper will attempt to perform the equivalent of result.getPropertyName().setSubPropertyName(value). The BeanWrapper instance used for this purpose is set to autogrow nested paths so that any objects in the path not yet constructed will be created and set, avoiding attempts to set values on null objects.

Author:
rvanluinen

Field Summary
 
Fields inherited from class org.springframework.jdbc.core.BeanPropertyRowMapper
logger
 
Constructor Summary
ConversionServiceAwareBeanPropertyRowMapper(Class<T> mappedClass, org.springframework.core.convert.ConversionService conversionService)
           
 
Method Summary
protected  void initBeanWrapper(org.springframework.beans.BeanWrapper bw)
           
 T mapRow(ResultSet rs, int rowNumber)
           
 
Methods inherited from class org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper
newInstance
 
Methods inherited from class org.springframework.jdbc.core.BeanPropertyRowMapper
getColumnValue, getMappedClass, initialize, isCheckFullyPopulated, isPrimitivesDefaultedForNullValue, setCheckFullyPopulated, setMappedClass, setPrimitivesDefaultedForNullValue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConversionServiceAwareBeanPropertyRowMapper

public ConversionServiceAwareBeanPropertyRowMapper(Class<T> mappedClass,
                                                   org.springframework.core.convert.ConversionService conversionService)
Method Detail

mapRow

public T mapRow(ResultSet rs,
                int rowNumber)
         throws SQLException
Specified by:
mapRow in interface org.springframework.jdbc.core.RowMapper<T>
Overrides:
mapRow in class org.springframework.jdbc.core.BeanPropertyRowMapper<T>
Throws:
SQLException

initBeanWrapper

protected void initBeanWrapper(org.springframework.beans.BeanWrapper bw)
Overrides:
initBeanWrapper in class org.springframework.jdbc.core.BeanPropertyRowMapper<T>


Copyright © 2013. All Rights Reserved.