Oracle读取Blob数据-通过hibernate

通过hibernate向Oracle存储字节类型的数据(如byte[]等),在定义实体对象的时候不能用"private byte[] content", 这样定义我试过,在存储数据的时候(session.save(user))是没有问题的,但是在读取Blob字段(Oracle中存储byte[]使用的是"BLOB"类型)时就会出现问题,读出来的东西就成了乱码.

 

使用hibernate读取Blob字段时,实体对象(对应的byte[]类型字段)应该这样定义:

import java.io.Serializable;
import java.sql.Blob;

public class User implements Serializable {                
	// Fields                  
	private long id;           
	private String name;           
	private String email;           
	private String addr;           
	// 定义Blob的pthto           
	private Blob photo;
	
	// getter and setters
	......
}


对应的hibernate文件配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<Hibernate-mapping>         
	<class name="com.xxx.xxx.User" table="user">             
		<id name="id" type="java.lang.Long">                 
			<column name="id" />                 
			<generator class="increment" />             
		</id>             
		<property name="name" type="java.lang.String">                 
			<column name="name" length="45" not-null="true" />             
		</property>             
		<property name="email" type="java.lang.String">                 
			<column name="email" length="45" />             
		</property>             
		<property name="addr" type="java.lang.String">                 
			<column name="addr" length="45" />             
		</property>             
		<!-- 映射blob类型 -->             
		<property name="photo" type="java.sql.Blob">                 
			<column name="photo" />             
		</property>         
	</class>     
</Hibernate-mapping>  

 

在hibernate.cfg.xml文件中一定要添加这句话

	<!--这句一定要加上,否则会报错,因为Oracle JDBC不允许流操作以批量方式进行,因此要关闭-->   
  	<property name="hibernate.jdbc.batch_size">0</property>


 


读取Blob数据方法:

// 写方法
public void testCreate(){                      
	User user = new User();          
	user.setName("linweiyang");          
	user.setAddr("beijing");          
	user.setEmail("linweiyang@163.com");          
	Blob photo = null;                  
	try {              
		//将图片读进输入流              
		FileInputStream fis = new FileInputStream("c:\\a.jpg");              
		//转成Blob类型              
		photo = Hibernate.createBlob(fis);                          
	} catch (FileNotFoundException e) {              
		e.printStackTrace();          
	} catch (IOException e) {              
		e.printStackTrace();          
	}                          
	user.setPhoto(photo);                     
	Session session = factory.openSession();          
	Transaction tr = session.beginTransaction();          
	session.save(user);          
	tr.commit();          
	session.close();         
}              

// 读方法
public void testRerieve(){                      
	Session session = factory.openSession();          
	User user = (User)session.load(User.class, new Long(3));          
	try {              
		//从数据库中要读取出来              
		InputStream is = user.getPhoto().getBinaryStream();              
		//在把写到一个图片格式的文件里              
		FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg");                          
		byte[] buffer = new byte[1024];              
		int len = 0;              
		//从数据库中读取到指定的字节数组中              
		while((len = is.read(buffer) )!= -1){                  
			//从指定的数组中读取,然后输出来,所以这里buffer好象是连接inputStream和outputStream的一个东西                  
			fos.write(buffer,0,len);              
		}          
	} catch (FileNotFoundException e) {              
		e.printStackTrace();          
	} catch (SQLException e) {              
		e.printStackTrace();          
	} catch (IOException  e) {              
		e.printStackTrace();          
	}                     
	session.close();      
}   


关于输入输出流

读入流自然要有读入的源头,输出也要输出到某个地方,输出一般是先要输读入,这里连接输入和输出的是一个在内存中的字节数组buffer.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值