textarea的内容保存到blob,然后读到textarea中显示出来

以下包括从网络上获取的内容和自己的一些理解,
可能存在不正确的地方,如遇到请指出,谢谢。

[color=red]----------------------------------------------------------------SEPARATE-LINE----------------------------------------------------------------[/color]

万物起因

不允许的操作: streams type cannot be used in batching

operation not allowed: streams type cannot be used in batching



场景

Hibernate 2.1.4
[url=http://www.iteye.com/problems/27144]将textarea中的值保存到Oracle的blob中,然后将blob保存的值显示在textarea中[/url]
http://www.iteye.com/problems/27144
当字节数超过一定大小时则出现如上的异常

BLOB指oracle.sql.BLOB,实现了java.sql.Blob
BlobImpl指hibernate.lob.BlobImpl,同样实现了java.sql.Blob

网上很多人都说将Hibernate的参数jdbc.batch_size的值设置为0即可解决问题
但我没有这么做
于是开始寻找解决办法
经典老帖"[url=http://www.iteye.com/topic/254]使用JDBC和Hibernate来写入Blob型数据到Oracle中[/url]":http://www.iteye.com/topic/254

我的想法一般都比较简单string2blob和blob2string就应该能解决问题
这其中要通过字节数组byte[]来过渡

于是想应该将Blob转成BLOB然后通过getBytes方法就可以拿到bytes
blob强转为BLOB报[url=http://www.iteye.com/topic/254?page=3#126746]ClassCastException[/url]
Debug一看原来运行中的Blob是BlobImpl类型的
强转为BLOB当然产生ClassCastException

既然发现运行中的类型为BlobImpl,所以强转
因为看起来BlobImpl也有getBytes方法
if (true == blob instanceof BlobImpl) {
BlobImpl bb = (BlobImpl) blob;
bytes = bb.getBytes(0, (int) bb.length());

但无法调用BlobImpl.getBytes方法,出现如下异常
java.lang.UnsupportedOperationException: Blob may not be manipulated from creating session

还是[url=http://66.102.11.99/]Google[/url]
原来Blob类型的只能用两个方法
[url]https://forum.hibernate.org/viewtopic.php?t=933977[/url]
[quote]As u can see out of the sourcecode, every method do nothing but:
Code:
throw new UnsupportedOperationException("Blob may not be manipulated from creating session");

The only to methods who are implemented really, are:
getBinaryStream()

length()


So use those instead of getBytes()

Why they used the text "Blob may not be manipulated" i can't say...
Maybe anybody can help us with that?

Greets[/quote]

于是继续改代码

public static byte[] getBytes(Blob blob) throws SerialException,
IOException, SQLException {
if (null == blob || true != blob instanceof Blob)
throw new IllegalArgumentException("LOBUtil.getBytes方法无法接受你传入的参数:"
+ blob);
byte[] bytes = null;
if (true == blob instanceof BlobImpl) {
InputStream is = blob.getBinaryStream();
bytes = IOUtil.newInstance().getBytes(is);
} else if (true == blob instanceof BLOB) {
bytes = ((BLOB) blob).getBytes();
}
return bytes;
}


这样就应该没啥大问题了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值