Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting int[] into PostgreSql with iBatis

... is there an easy way to insert a Java int[] into PostgreSql with the help of iBatis? (the older one, not the new MyBatis)

Not sure if I DO need a custom type handler or not, but I'm having a difficult time finding a code sample that would illustrate what's going on.

Thanks in advance.

ps:

since the original posting, I'm able to read the array from DB and populate the int[] in the domain object. But can't write to the db yet :-(

so in the domain model there's:

int[] crap = null;

with getters and setters, cusom property handler looks like this:

public class ArrayTypeHandler implements TypeHandlerCallback {
public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {

    if( parameter == null){
        setter.setNull( Types.ARRAY);
    } else {
        setter.setArray( (Array) Arrays.asList(parameter ) );
    }

}

public Object getResult(ResultGetter getter) throws SQLException {
    Array array = getter.getResultSet().getArray(getter.getColumnName());
    if( !getter.getResultSet().wasNull()){
         return array.getArray();
    } else { return null; }

}

public Object valueOf(String string) {
    throw new UnsupportedOperationException("Not supported yet.");
}

}

sqlMapConfig.xml:

<typeHandler javaType="java.sql.Array" jdbcType="ARRAY" callback="project.persistance.sqlmapdao.ArrayTypeHandler"  />

When trying to update i get the following error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];   

--- The error occurred in project/persistance/sql_xml/Article.xml.
--- The error occurred while applying a parameter map.
--- Check the updateArticle-InlineParameterMap.
--- Check the parameter mapping for the 'crap' property.
--- Cause: java.lang.NullPointerException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in project/persistance/sql_xml/Article.xml.
--- The error occurred while applying a parameter map.
--- Check the updateArticle-InlineParameterMap.
--- Check the parameter mapping for the 'crap' property.
--- Cause: java.lang.NullPointerException

... any hints as to what I'm missing? thanks

===

... worked my way up to ClassCastExceptiong :-)

trying to set the propery:

    public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
    int[] c = (int[]) parameter;

    setter.setArray( (java.sql.Array) c  );
}

... and the ensuing exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];   

--- The error occurred in project/persistance/sql_xml/Article.xml.
--- The error occurred while applying a parameter map.
--- Check the updateArticle-InlineParameterMap.
--- Check the parameter mapping for the 'crap' property.
--- Cause: java.lang.ClassCastException: java.util.ArrayList; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in project/persistance/sql_xml/Article.xml.
--- The error occurred while applying a parameter map.
--- Check the updateArticle-InlineParameterMap.
--- Check the parameter mapping for the 'crap' property.
--- Cause: java.lang.ClassCastException: java.util.ArrayList

... I've had it today though. Thanks

like image 512
vector Avatar asked Jul 24 '26 19:07

vector


1 Answers

This looks promising:

http://beerholder.blogspot.com/2007/10/mapping-postgresql-arrays-with-ibatis-i.html

like image 152
Jeremy Avatar answered Jul 27 '26 14:07

Jeremy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!