数据库字段为BLOB类型,Hibernate的xml映射文件配置成type="binary",在业务类中得到调用DAO获取一个对象,发现对象的blob字段大小一直等于64,输出是乱码!!郁闷了一天!急请高人解决!系统架构Struts+Hibernate3.2+String2.0,
数据库oracle 10g,
jar驱动程序ojdbc14.jar,版本10.2.0.3.0。
数据库oracle 10g,
jar驱动程序ojdbc14.jar,版本10.2.0.3.0。
我用select length(attachment) from pub_user_files where oid = 27,输出大小等于716。数据库中存储数据正常,可以用工具软件存储成正确文件,就是用hibernate的DAO获取对象时,大小只有64k。什么缘故呢?
/** 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>