数据库字段为BLOB类型,Hibernate的xml映射文件配置成type="binary",在业务类中得到调用DAO获取一个对象,发现对象的blob字段大小一直等于64,输出是乱码!!郁闷了一天!急请高人解决!系统架构Struts+Hibernate3.2+String2.0,
数据库oracle 10g,
jar驱动程序ojdbc14.jar,版本10.2.0.3.0。

解决方案 »

  1.   

    你直接进数据库用select length(blob字段) from t 看看数据是否正常。BLOBL一般放二进制数据,你输出的是什么。
      

  2.   

    回复vc555:
    我用select   length(attachment)   from   pub_user_files where oid = 27,输出大小等于716。数据库中存储数据正常,可以用工具软件存储成正确文件,就是用hibernate的DAO获取对象时,大小只有64k。什么缘故呢?
      

  3.   

    问题已经解决,代码如下:private byte[] attachment;
    /** Don't invoke this. Used by Hibernate only. */
    public void setAttachmentBlob(Blob attachmentBlob) {
    this.attachment = this.toByteArray(attachmentBlob);
    } /** Don't invoke this. Used by Hibernate only. */
    public Blob getAttachmentBlob() {
    return Hibernate.createBlob(this.attachment);
    } private byte[] toByteArray(Blob fromBlob) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
    return toByteArrayImpl(fromBlob, baos);
    } catch (SQLException e) {
    throw new RuntimeException(e);
    } catch (IOException e) {
    throw new RuntimeException(e);
    } finally {
    if (baos != null) {
    try {
    baos.close();
    } catch (IOException ex) {
    }
    }
    }
    } private byte[] toByteArrayImpl(Blob fromBlob, ByteArrayOutputStream baos)
    throws SQLException, IOException {
    byte[] buf = new byte[4000];
    InputStream is = fromBlob.getBinaryStream();
    try {
    for (;;) {
    int dataSize = is.read(buf);
    if (dataSize == -1)
    break;
    baos.write(buf, 0, dataSize);
    }
    } finally {
    if (is != null) {
    try {
    is.close();
    } catch (IOException ex) {
    }
    }
    }
    return baos.toByteArray();
    }
    XML:
    <!-- MSSqlServer -->
            <!-- property name="attachment" type="binary">
                <column name="ATTACHMENT" />
            </property -->
            <!-- Oracle -->
            <property name="attachmentBlob" type="blob">
                <column name="ATTACHMENT" />
            </property>