您好,欢迎访问一九零五行业门户网

使用Hibernate处理Oracle中的Blob字段

写入blob字段和写入其它类型字段的方式非常不同,因为blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入blob
1. bolb类型字段说明:
写入blob字段和写入其它类型字段的方式非常不同,因为blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入blob之前,必须获得cursor才能进行写入,那么如何获得blob的cursor呢?
这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor用select查询出来,这样通过两步操作,你就获得了blob的cursor,,可以真正的写入blob数据了。
2. bolb类型字段保存:
hibernate的配置文件就不写了 ,需要将blob字段了类型设为java.sql.blob,下面直接上代码
 public void save(zyglblxx bean, inputstream ins) throws webserviceexception {
  // todo auto-generated method stub
  session session = this.gethibernatetemplate().getsessionfactory().opensession();
  //ins.
  //out.write(b)
  try {
   bean.setdoccontent(blob.getemptyblob());
transaction tr = session.begintransaction();
   bean.setvid(webserviceeditutils.getpk());
   session.save(bean);
   session.flush();
   session.refresh(bean, lockmode.upgrade);
   if(ins!=null){
    serializableblob sb = (serializableblob)bean.getdoccontent();
    blob b = (blob)sb.getwrappedblob();
outputstream out = b.getbinaryoutputstream();
int len=-1;
    byte[] bt = new byte[2048];        //可以根据实际情况调整,建议使用1024,即每次读1kb
    while((len=(ins.read(bt))) != -1)      {
     out.write(bt,0,len);                    //建议不要直接用os.write(bt)
    }
    out.flush();
    ins.close();
    out.close();
   }
   session.flush();
   tr.commit();
   session.close();
  } catch (ioexception e) {
   e.printstacktrace();
   throw exceptionmanager.getexcption(18);
  } catch (sqlexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
   throw exceptionmanager.getexcption(19);
  }
 }
为了保存文件时比较方便我这里的参数直接接收为inputstream,其他类型类似
hibernate 的详细介绍:请点这里
hibernate 的下载地址:请点这里
hibernate 中文手册 pdf
其它类似信息

推荐信息