Oracle读取Blob数据-通过hibernate


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

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

  1. import java.io.Serializable;  
  2. import java.sql.Blob;  
  3.   
  4. public class User implements Serializable {                  
  5.     // Fields                     
  6.     private long id;             
  7.     private String name;             
  8.     private String email;             
  9.     private String addr;             
  10.     // 定义Blob的pthto              
  11.     private Blob photo;  
  12.       
  13.     // getter and setters   
  14.     ......  
  15. }  

对应的hibernate文件配置:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <Hibernate-mapping>           
  5.     <class name="com.xxx.xxx.User" table="user">               
  6.         <id name="id" type="java.lang.Long">                   
  7.             <column name="id" />                   
  8.             <generator class="increment" />               
  9.         </id>               
  10.         <property name="name" type="java.lang.String">                   
  11.             <column name="name" length="45" not-null="true" />               
  12.         </property>               
  13.         <property name="email" type="java.lang.String">                   
  14.             <column name="email" length="45" />               
  15.         </property>               
  16.         <property name="addr" type="java.lang.String">                   
  17.             <column name="addr" length="45" />               
  18.         </property>               
  19.         <!-- 映射blob类型 -->               
  20.         <property name="photo" type="java.sql.Blob">                   
  21.             <column name="photo" />               
  22.         </property>           
  23.     </class>       
  24. </Hibernate-mapping>    

读取Blob数据方法:

  1. // 写方法   
  2. public void testCreate(){                        
  3.     User user = new User();            
  4.     user.setName("linweiyang");            
  5.     user.setAddr("beijing");            
  6.     user.setEmail("linweiyang@163.com");            
  7.     Blob photo = null;                    
  8.     try {                
  9.         //将图片读进输入流                 
  10.         FileInputStream fis = new FileInputStream("c:\\a.jpg");                
  11.         //转成Blob类型                 
  12.         photo = Hibernate.createBlob(fis);                            
  13.     } catch (FileNotFoundException e) {                
  14.         e.printStackTrace();            
  15.     } catch (IOException e) {                
  16.         e.printStackTrace();            
  17.     }                            
  18.     user.setPhoto(photo);                       
  19.     Session session = factory.openSession();            
  20.     Transaction tr = session.beginTransaction();            
  21.     session.save(user);            
  22.     tr.commit();            
  23.     session.close();           
  24. }                
  25.   
  26. // 读方法   
  27. public void testRerieve(){                        
  28.     Session session = factory.openSession();            
  29.     User user = (User)session.load(User.classnew Long(3));            
  30.     try {                
  31.         //从数据库中要读取出来                 
  32.         InputStream is = user.getPhoto().getBinaryStream();                
  33.         //在把写到一个图片格式的文件里                 
  34.         FileOutputStream fos = new FileOutputStream("c:\\linweihan.jpg");                            
  35.         byte[] buffer = new byte[1024];                
  36.         int len = 0;                
  37.         //从数据库中读取到指定的字节数组中                 
  38.         while((len = is.read(buffer) )!= -1){                    
  39.             //从指定的数组中读取,然后输出来,所以这里buffer好象是连接inputStream和outputStream的一个东西                     
  40.             fos.write(buffer,0,len);                
  41.         }            
  42.     } catch (FileNotFoundException e) {                
  43.         e.printStackTrace();            
  44.     } catch (SQLException e) {                
  45.         e.printStackTrace();            
  46.     } catch (IOException  e) {                
  47.         e.printStackTrace();            
  48.     }                       
  49.     session.close();        
  50. }     

关于输入输出流

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

相关内容