hibernate操作clob大字段,网上有很多解决办法。我看过很多。但最后都放弃了。太麻烦了。
最后我的解决办法是从session中取得connection,用connection操作大字段。本来大字段就不是一个通用的东东,所以这样写也是没办法的办法。
本方法个人不推荐使用。实在没办法时可以一试。
下面是示例代码:
/**
* 写邮件正文clob字段
*
* @param sess
* @param msg_id
* @param text
* @throws Exception
*/
private void saveMessageText(Session sess, String msg_id, String text) throws Exception {
//写入新闻内容,大字段
try {
//sess.connection().setAutoCommit(false);
java.sql.Statement stmt = sess.connection().createStatement();
String sql = "update t_plat_message set msg_text = EMPTY_CLOB() where msg_id ='" + msg_id + "'";
stmt.executeUpdate(sql);
sql = "select msg_text from t_plat_message where msg_id='" + msg_id + "' for update";
ResultSet rs = stmt.executeQuery(sql);
CLOB Clob = null;
if (rs.next()) {
Clob = (CLOB) rs.getClob(1);
Writer writer = Clob.getCharacterOutputStream();
writer.write(text);
writer.flush();
writer.close();
}
rs.close();
stmt.close();
} catch (Exception e) {
throw e;
}
}