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;
}


這樣就應該沒啥大問題了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章