Spring JDBC BeanPropertyRowMapper yes no ('Y','N') to boolean bean properties Spring JDBC BeanPropertyRowMapper yes no ('Y','N') to boolean bean properties oracle oracle

Spring JDBC BeanPropertyRowMapper yes no ('Y','N') to boolean bean properties


So I figured out how to do this. I extended BeanPropertyRowMapper and handler boolean types through some custom code before handing off the control to beanpropertyrowmapper for rest of the data types.

Note: It works for me because I use oracle and all of the 'boolean' type columns are strings with 'y','yes','n' & 'no' type values.

Those who use numerical 1,0 or other formats could potentially improve it further by making it generic through an object yes map and getting objects from resultset and looking them up in this map. Hope this helps someone else in a situation like mine.

import java.beans.PropertyDescriptor;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Arrays;import java.util.HashSet;import java.util.Set;import org.apache.commons.lang3.StringUtils;import org.springframework.jdbc.core.BeanPropertyRowMapper;/** * Extends BeanPropertyRowMapper to allow for boolean fields * mapped to 'Y,'N' type column to get set correctly. Using stock BeanPropertyRowMapper * would throw a SQLException. *  */public class ExtendedBeanPropertyRowMapper<T> extends BeanPropertyRowMapper<T> {    //Contains valid true values    public static final Set<String> TRUE_SET = new HashSet<String>(Arrays.asList("y", "yes", "true"));    public ExtendedBeanPropertyRowMapper(Class<T> class1) {        super(class1);    }    @Override    /**     * Override <code>getColumnValue</code> to add ability to map 'Y','N' type columns to     * boolean properties.     *      * @param rs is the ResultSet holding the data     * @param index is the column index     * @param pd the bean property that each result object is expected to match     * (or <code>null</code> if none specified)     * @return the Object value     * @throws SQLException in case of extraction failure     * @see org.springframework.jdbc.core.BeanPropertyRowMapper#getColumnValue(java.sql.ResultSet, int, PropertyDescriptor)      */    protected Object getColumnValue(ResultSet rs, int index,            PropertyDescriptor pd) throws SQLException {        Class<?> requiredType = pd.getPropertyType();        if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) {            String stringValue = rs.getString(index);            if(!StringUtils.isEmpty(stringValue) && TRUE_SET.contains(stringValue.toLowerCase())){                return true;            }            else return false;        }               return super.getColumnValue(rs, index, pd);    }}


BeanPropertyRowMapper will convert values into a Boolean object with 0=false and 1=true. Just tried this and it works.

This blog post has more information, as well as code examples in Java and C with OCCI.


Old question, but you can do something like

public void setIsActive(String active) {    this.active = "Y".equalsIgnoreCase(active);}