Spring Hbernate DB2 Blob字段的保存更新试验

/src/com/lwq/study/hib/imp/TestBlob.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.lwq.study.hib.imp">
    <class name="TestBlob" table="TEST.TBLOB">
        <id name="id" column="BID" type="long">
            <generator class="increment"/>
        </id>
        <property name="name" type="string"/>
        <property name="fieldBlob" column="FBLOB" type="blob"/>
    </class>
</hibernate-mapping>

/src/com/lwq/study/hib/imp/TestBlob.java

package com.lwq.study.hib.imp;
import java.io.Serializable;
import java.sql.Blob;

public class TestBlob implements Serializable {
    private Long id;
    private String name;
    private Blob fieldBlob;


    public Blob getFieldBlob() {
        return fieldBlob;
    }
    public void setFieldBlob(Blob fieldBlob) {
        this.fieldBlob = fieldBlob;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
  
}

/src/com/lwq/study/hib/IDaoTest_BLob.java

package com.lwq.study.hib;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;

import org.hibernate.Hibernate;

import com.lwq.study.hib.imp.TestBlob;
import com.lwq.study.hib.junit.AbstractTestBean;

public class IDaoTest_BLob extends AbstractTestBean {
    IDao dao = null;
    protected void setUp() throws Exception {
        super.setUp();
        dao = (IDao) applicationContext.getBean("dao");
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    /***************************************************************************
     *
     * <p>
     * 测试Blob字段
     * </p>
     *
     * @param
     * @exception
     * @return
     */
    public void test() {
        /*保存含blob的对象*/
        TestBlob testBlob = new TestBlob();
        String strBlob = "我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存我保存";
        Blob fBlob = createBlob(strBlob);
        testBlob.setName("名称");
        testBlob.setFieldBlob(fBlob);
        dao.saveTestBlob(testBlob);
       
        /*获取含blob的对象*/
        Long blobID = testBlob.getId();
        TestBlob testBlob2 = (TestBlob) dao.getTestBlob(blobID);
        String strBlob2 = getString(testBlob2.getFieldBlob());
        System.out.println(strBlob2);
       
        /*更新含blob的对象*/
        String strForUpdate = "我更新我更新我更新我更新我更新我更新我更新我更新我更新我更新我更新我更新我更新我更新我更新";
        Blob fBlobUpdate = createBlob(strForUpdate);
        testBlob2.setFieldBlob(fBlobUpdate);
        dao.update(testBlob2);
        TestBlob testBlob3 = (TestBlob) dao.getTestBlob(blobID);
        String strBlob3 = getString(testBlob3.getFieldBlob());
        System.out.println(strBlob3);
       
    }

    private Blob createBlob(String idea) {
        byte[] content = null;
        if (idea == null || "".equals(idea)) {
            return null;
        }

        try {
            content = idea.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Blob blob = Hibernate.createBlob(content);
        return blob;
    }

    private String getString(Blob blob) {
        String contentStr = null;
        InputStream input = null;
        ByteArrayOutputStream baos = null;
        try {
            input = blob.getBinaryStream();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            baos = new ByteArrayOutputStream();
            int i = -1;
            while ((i = input.read()) != -1) {
                baos.write(i);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            contentStr = new String(baos.toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return contentStr;
    }

}

src/com/lwq/study/hib/imp/Dao.java

......

    public void saveTestBlob(TestBlob testBlob){
        getHibernateTemplate().save(testBlob);
    }
   
    public void updateTestBlob(TestBlob testBlob){
        getHibernateTemplate().update(testBlob);
    }
   
    public TestBlob getTestBlob(Long eBlobID){
        return (TestBlob) getHibernateTemplate().load(TestBlob.class, eBlobID);
    }

你可能感兴趣的:(DAO,spring,exception,String,db2,null)