iBatis 2 Hints

To use UUID/Guid fields on iBatis 2

A new type handler must be added to handle UUID fields (Guid for Windows users).

public class UuidTypeHandler extends BaseTypeHandler implements TypeHandler {

    @Override
    public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType) throws SQLException {
        ps.setObject(i, parameter);
    }

    @Override
    public Object getResult(ResultSet rs, String columnName) throws SQLException {
        String s = rs.getString(columnName);
        if (s == null) {
            return null;
        } else {
            return UUID.fromString(s);
        }
    }

    @Override
    public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
        String s = rs.getString(columnIndex);
        if (s == null) {
            return null;
        } else {
            return UUID.fromString(s);
        }
    }

    @Override
    public Object getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String s = cs.getString(columnIndex);
        if (s == null) {
            return null;
        } else {
            return UUID.fromString(s);
        }
    }

    @Override
    public Object valueOf(String s) {
        return UUID.fromString(s);
    }
}

It must be initialized inside the sqlMapConfig.xml file into te iBatis configuration

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
    ...
    <typeHandler javaType="java.util.UUID" callback="com.tierraservice.cnh.commons.UuidTypeHandler"/>
    ...
</sqlMapConfig>

Last modified on: November 27, 2015