以下为Hibernate Blob数据类型映射的一个例子,通过例子来把握Hibernate Blob数据类型映射。

说明:BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据。BLOB类型实际是个类型系列(TinyBlob、Blob、MediumBlob、LongBlob),除了在存储的最大信息量上不同外,他们是等同的。

以MySQL数据库为例,有四种BLOB类型

TinyBlob 最大 255
Blob 最大 65K
MediumBlob 最大 16M
LongBlob 最大 4G

实际使用中根据需要存入的数据大小定义不同的BLOB类型。
需要注意的是:如果你存储的文件过大,数据库的性能会下降很多。

public class Serviceinfo implements java.io.Serializable {   

    private Integer sid;   
        private String serviceName;   
        private Blob wsdl;   

        public Serviceinfo() {}   

        public Integer getSid() {   
        return this.sid;   
    }   
    public void setSid(Integer sid) {   
        this.sid = sid;   
    }   

    public String getServiceName() {   
        return this.serviceName;   
    }   
    public void setServiceName(String serviceName) {   
        this.serviceName = serviceName;   
    }   

        public Blob getWsdl() {   
        return wsdl;   
    }   
    public void setWsdl(Blob wsdl) {   
        this.wsdl = wsdl;   
    }   
}




Serviceinfo.hbm.xml



<hibernate-mapping>   
    <class name="cn.edu.tju.ikse.sn.bean.Serviceinfo" table="serviceinfo" catalog="sn">   
        <id name="sid" type="integer">   
            <column name="sid" />   
            <generator class="increment" />   
        </id>   
        <property name="serviceName" type="string">   
            <column name="service_name" length="256" />   
        </property>   
        <property name="wsdl" type="blob">   
            <column name="wsdl" />   
        </property>   
     </class>   
</hibernate-mapping>





测试代码


testCreate将getWeather.wsdl文件写入blob字段


testRerieve从blob字段中读取到出来,写到一个文件中



public void testCreate(){                     
    Serviceinfo s= new Serviceinfo();         
    s.setName("getWeather");             
    Blob wsdl= null;                 
    try {         
       //将wsdl文件读进输入流         
        FileInputStream fis = new FileInputStream("c:\\getWeather.wsdl");         
        //转成Blob类型         
        photo = Hibernate.createBlob(fis);                         
    } catch (FileNotFoundException e) {         
        e.printStackTrace();         
    } catch (IOException e) {         
        e.printStackTrace();         
   }                         
    s.setWsdl(wsdl);                    
    Session session = HibernateSessionFactory.getSession();      
    Transaction tr = session.beginTransaction();         
    session.save(s);         
    tr.commit();         
    session.close();            
}





public void testRerieve {   
        Session session = HibernateSessionFactory.getSession();   
        Serviceinfo s = (Serviceinfo) session.load(Serviceinfo.class, new Integer(1));   
        try {   
            // 从数据库中要读取出来   
            InputStream is = s.getWsdl().getBinaryStream();   
            // 在把写到一个txt的文件里   
            FileOutputStream fos = new FileOutputStream("c:\\wsdl.txt");   
            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.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里。以上便是Hibernate Blob数据类型映射的一个例子。