ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From LinaTomy <ltho...@taxcient.com>
Subject Re: Insert oracle BLOB's with iBatis
Date Fri, 26 Sep 2008 01:05:52 GMT

     This is how I do it. Hope it works for you

My java bean class has two attributes
 Blob myBlob;
 InputStream myStream;

 <typeAlias alias="myBean" type="MyBean"/>
 <resultMap id="myResult" class="myBean">
    <result column="Blob_Column" jdbcType="BLOB" property="myBlob" />

 <parameterMap id="insertMap" class="myBean">
    <parameter property="myBlob" jdbcType="BLOB" javaType="java.sql.Blob"
mode="OUT" />

Custom type handler
import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

import java.sql.Blob;
import java.sql.SQLException;

public class BlobTypeHandlerCallback implements TypeHandlerCallback
  private static final byte BLOB_NULL = 127;

  private static byte[] blobNull = null;

  public Object getResult(ResultGetter getter) throws SQLException
    Blob b = null;
      b = getter.getBlob();
    catch (Exception ex)
      throw new SQLException("Unable to read the blob: " + ex.toString());
    return b;

  public void setParameter(ParameterSetter setter,
    Object parameter) throws SQLException
    setter.setBlob((Blob) parameter);

  public Object valueOf(String s)
    return s;

  private byte[] getBlobNull()
    if (blobNull == null)
      byte[] bn = new byte[1];
      bn[0] = BLOB_NULL;
      blobNull = bn;
    return blobNull;

For inserts I use select for update after I insert and keep my BLOB as an
INOUT parameter. Then i get an OutputStream to the Blob and write my file
contents to it.
For selects this works fine. I get the contents of the Blob.

AbelGG wrote:
> Hi!, I've a DB Oracle 9i and I want to insert a BLOB using iBatis. I've
> mapped a BLOB column as follows:
> <result column="FICHERO" jdbcType="BLOB" property="fichero" javaType="[B"
> />
> The java class that I mapped here has the property "fichero" as byte[]
> with getter's and setter's.
> The iBatis versión that I'm using is 2.3.0 and the oracle driver
> ojdbc14.jar (is the version from Oracle 10g Release 2 because I read that
> 9i's version had errors and I have to use the latest ojdbc14.jar although
> it was not the version for Oracle 9i).
> When I try to insert a BLOB it failed and tell me the follow:
> java.sql.SQLException: ORA-00932: inconsistent datatypes: expected BINARY
> got NUMBER.
> I tried to use a typeHandler in the mapping:
> typeHandler="org.springframework.orm.ibatis.support.BlobByteArrayTypeHandler"
> . But the result is the same.
> ¿Can anyone help me? Thanks. 

View this message in context: http://www.nabble.com/Insert-oracle-BLOB%27s-with-iBatis-tp19623711p19680960.html
Sent from the iBATIS - User - Java mailing list archive at Nabble.com.

View raw message