使用Hibernate处理Oracle中的Blob字段

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jaune161/article/details/19235087
1. Bolb类型字段说明: 
写入Blob字段和写入其它类型字段的方式非常不同,因为Blob自身有一个cursor,你必须使用cursor对blob进行操作,因而你在写入Blob之前,必须获得cursor才能进行写入,那么如何获得Blob的cursor呢?

这需要你先插入一个empty的blob,这将创建一个blob的cursor,然后你再把这个empty的blob的cursor用select查询出来,这样通过两步操作,你就获得了blob的cursor,可以真正的写入blob数据了。

参考 http://blog.csdn.net/jionghan3855/article/details/1964386

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,其他类型类似

3. Bolb类型字段读取:
public ZyglBlxx getBlxx(Map<String,Object> map){
		Session session = this.getHibernateTemplate().getSessionFactory().openSession();
		ZyglBlxx bean = null;
		Criteria criteria = session.createCriteria(ZyglBlxx.class);
		Set<String> set = map.keySet();
		Iterator<String> it = set.iterator();
		while(it.hasNext()){
			String key = it.next();
			Object value = map.get(key);
			criteria.add(Property.forName(key).eq(value));
		}
		List<ZyglBlxx> list = criteria.list();
		if(list.size()>0)bean = list.get(0);
		session.close();
		return bean;
	}
从数据库的读取跟以前一样
字段的读取有点麻烦

public String blobToString(Blob inblob){
		String data = null;
		if(inblob == null)return data;
		SerializableBlob seb = (SerializableBlob)inblob;
		try {
			
			BLOB blob = (BLOB)seb.getWrappedBlob();
			BufferedInputStream stream = new BufferedInputStream(blob.getBinaryStream());
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			byte[] buff = new byte[1024];
			int i = 0;
		    while((i=stream.read(buff))!=-1){
		    	out.write(buff,0,i);
		    }
			
			data = out.toString();
			stream.close();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}
直接调用inblob.getBytes();得到的是乱码,blob.getBinaryStream().available()得到的是0.对于Hibernate3版本只能用上面的方法来解析。
4. 使用JPA2.0读写Bolb类型字段:
使用JPA2.0读写Blob类型就方便的多了,只需要将Blob类型改为byte[],然后在字段上加上@Basic和@Lob注解就可以了。

转载于:https://my.oschina.net/jaune161/blog/3015898

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值