使用 hibernate 存取大對象數據類型(clob和blob)


數據庫表如下:

book表

QQ截圖20131015201126

id 該表的主鍵。number類型。
photo 代表圖書的圖片,blob類型。
description 圖書的描述,clob類型。

使用 hibernate3 往 book 表插入Clob,Blob數據

省略hibernate配置文件,實體映射文件和實體類代碼,直接帖實現代碼:

package accp.hibernate;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Clob;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Client {

    public static void main(String[] args) throws IOException {
        //得到session
        Configuration cfg = new Configuration().configure();
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        //得到圖片的blob
        InputStream in = new FileInputStream("F:\\4563123.jpg");
        Blob blob = Hibernate.createBlob(in);
        //得到簡介的clob
        Clob clob = Hibernate.createClob("這是一本書和詳細描述。#(*&#@¥%(*&@¥)(@#¥#¥");
        //創建圖書對象
        Book book = new Book();
        book.setPhoto(blob);
        book.setDescription(clob);
        //存進數據庫
        session.beginTransaction();
        session.save(book);
        session.getTransaction().commit();
        session.close();
    }

}

    使用hibernate的靜態方法createBlob()得到Blob對象。createBlob方法需要一個InputStream對象作為參數。new一個FileInputStream作為它的參數,假設在F盤有一個圖片文件名為4563123.jpg。

    使用hibernate的靜態方法createClob()得到Clob對象。createClob方法直接傳入字符串即可。

使用 hibernate3 從 book 表取出Clob,Blob數據

省略hibernate配置文件,實體映射文件和實體類代碼,直接帖實現代碼:

 1 package accp.hibernate;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.io.OutputStream;
 7 import java.io.Reader;
 8 import java.sql.SQLException;
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.cfg.Configuration;
12 
13 public class Client {
14 
15     public static void main(String[] args) throws IOException, SQLException {
16         //得到session
17         Configuration cfg = new Configuration().configure();
18         SessionFactory sf = cfg.buildSessionFactory();
19         Session session = sf.openSession();
20 
21         //從數據庫里取值
22         session.beginTransaction();
23         Book book = (Book)session.get(Book.class,22);
24         session.getTransaction().commit();
25         session.close();
26         //把簡歷打印到控制台
27         Reader reader = book.getDescription().getCharacterStream();
28         int i = reader.read();
29         while(i!=-1){
30             System.out.print((char)i);
31             i=reader.read();
32         }
33         reader.close();
34         //把圖片拷貝到D盤
35         InputStream in = book.getPhoto().getBinaryStream();
36         OutputStream out=new FileOutputStream("d:\\out.jpg");
37         byte[] b=new byte[1024];    
38         while((i=in.read(b))!=-1){
39             out.write(b);
40         }
41         out.close();
42         in.close();
43     }
44 
45 }

    21-25行代碼把數據從數據庫中取出,封裝到book對象中。

    為了驗證取值是否成功,第26-33行代碼把Clob對象的內容打印到控制台,通過Clob對象的getCharacterStream()方法得到包含 CLOB 數據的 java.io.Reader 對象,然后通過操作Reader對象把內容輸出。

    第34-42行代碼把Blob對象包含的內容拷貝到d:\out.jpg文件中。通過Blob對象的getBinaryStream()方法得到包含 BLOB 數據的流,通過操作流把內容拷貝。

使用 hibernate4 往 book 表插入Clob,Blob數據

hibernate4相比hibernate3要復雜一些,如下:

//省略import...........

public class Client {

    public static void main(String[] args) throws IOException {
        //得到session
        Configuration cfg = new Configuration().configure();
        ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry(); 
        SessionFactory sf = cfg.buildSessionFactory(sr);
        Session session = sf.openSession();
        //得到LobHelper
        LobHelper lobHelper = session.getLobHelper();
        //得到圖片的blob
        InputStream in = new FileInputStream("F:\\4563123.jpg");
        Blob blob = lobHelper.createBlob(in, in.available());
        //得到簡介的clob
        Clob clob = lobHelper.createClob("這是一本書。#(*&#@¥%(*&@¥)(@#¥#¥");
        //創建圖書對象
        Book book = new Book();
        book.setPhoto(blob);
        book.setDescription(clob);
        //存進數據庫
        session.beginTransaction();
        session.save(book);
        session.getTransaction().commit();
        session.close();
    }

}

      hibernate4中得到session對象的方法和hibernate3中略有不同,但用hibernate3的寫法也是可以的。

      在hibernate4中 hibernate.createBlob() 方法和 hibernate.createClob() 已經過時,取而代之的是LobHelper.createBlob()和lobHelper.createClob()。可以通過session.getLobHelper();得到LobHerlper對象。

取值 和hibernate3 基本一樣,這里不再演示!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM