Hibernate實現Oracle Blob/Clob類型資料讀寫

來源:互聯網
上載者:User

環境:Eclipse3.1+Hibernate2+Oracle 10g
說明:本例在使用Eclipse3.1通過一個Java項目來實現對Oracle中Blob/Clob類型資料的讀寫操作.
1.在oracle資料庫中建立一個表,取名:BIGDATA, sql語句如下:
  CREATE TABLE BIGDATA (id number NOT NULL,image blob,resume clob);//建立表
  ALTER TABLE BIGDATA ADD ( CONSTRAINT bigdata_pk PRIMARY KEY (id) USING INDEX );// 修改id為主鍵:

2.在Eclipse中建立一個java項目,取名:HibernateSample,項目中需要匯入的jar檔案可根據需要匯入即可。
  建立一個源檔案夾src,在src下建立一個包,取名bigDataAccess,在該包下建立一個類,取名DataModel.java,內容如下:
 package bigDataAccess;

import java.sql.Blob;
import java.sql.Clob;

public class DataModel {
 Long id;

 Blob image;

 Clob resume;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public Blob getImage() {
  return image;
 }

 public void setImage(Blob image) {
  this.image = image;
 }

 public Clob getResume() {
  return resume;
 }

 public void setResume(Clob resume) {
  this.resume = resume;
 }
}

2.再在bigDataAccess包下建立一個類取名:HibernateUtil.java用於管理session,原始碼如下:
package bigDataAccess;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;

public class HibernateUtil {
 private static SessionFactory sessionFactory;

 public static final ThreadLocal session= new ThreadLocal();
 static {
  try {
   // 執行個體化一個SessionFactory對象 
   //System.out.println("通過靜態模組建立一個SessionFactory");
   sessionFactory = new Configuration().configure()
     .buildSessionFactory();
  } catch (HibernateException ex) {
   throw new RuntimeException("Configuration problem:"
     + ex.getMessage(), ex);
  }
 } 
 public static Session currentSession() throws HibernateException {
  Session s = (Session) session.get();
  // 當原session為空白或已關閉時,打一個新的Session
  if (s == null || !s.isOpen()) {
   s = sessionFactory.openSession();
   session.set(s);
  }
  return s;
 }

 public static void closeSession() throws HibernateException {
  Session s = (Session) session.get();  
  if (s != null) {
   s.close();
  }  
 }
}

2.在包bigDataAccess下建立對應檔bigDataAccess.hbm.xml,內容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
 <class name="bigDataAccess.DataModel" table="BIGDATA">
  <id name="id" type="long" column="ID">
   <generator class="increment" />
  </id>
  <property name="image"  type="java.sql.Blob"  column="image" />
  <property name="resume" type="java.sql.Clob"  column="resume" />
 </class>
</hibernate-mapping>
3.建立Hibernate設定檔於src目錄下,hibernate.cfg.xml內容如下
 <?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
                                         "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
 <session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
  <property name="hibernate.connection.username">work</property>
  <property name="hibernate.connection.password">caecaodb</property>
  <property name="hibernate.show_sql">true</property>
  <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property>
<!--這句一定要加上,否則會報錯,因為Oracle JDBC不允許流操作以批量方式進行,因此要關閉-->   
  <property name="hibernate.jdbc.batch_size">0</property>
  <mapping resource="bigDataAccess/bigDataAccess.hbm.xml"/>  
 </session-factory>
</hibernate-configuration>
4.在包bigDataAccess下建立一個帶有main()函數的類,取名HibernateTest.java,原始碼如下:
package bigDataAccess;
//注意要匯入正確有包

public class HibernateTest{

 public HibernateTest() throws HibernateException, SQLException, IOException {
  doit();
 }

 public static void main(String[] args) throws HibernateException,
   SQLException, IOException {
  new HibernateTest();

 }

 public void doit() throws HibernateException, SQLException, IOException {

  // Blog,Clob類型資料讀寫---------write

  /*
   * Oracle Blob/Clob欄位本身有一個遊標(cursor),
   * JDBC通過遊標對Blob/Clob欄位進行操作,在Blob/Clob欄位被建立之前,
   * 無法獲得其遊標控制代碼,因此必須首先建立一個空Blob/Clob欄位,再從空這個空Blob/Clob欄位擷取遊標,再寫入要儲存的資料
   */
  System.out.println("寫入資料");
  Session session = HibernateUtil.currentSession();
  Transaction tx = session.beginTransaction();

  DataModel info = new DataModel();
  info.setImage(Hibernate.createBlob(new byte[1]));
  info.setResume(Hibernate.createClob(" "));// 這裡的參數是一個空格
  session.save(info);
  session.flush();// 強制執行insert
  // 通過refresh方法,強制Hibernate執行select for update
  session.refresh(info, LockMode.UPGRADE);

  // 向Blog寫入實際內容
  oracle.sql.BLOB blob = (oracle.sql.BLOB) info.getImage();
  FileInputStream imgis = new FileInputStream("d://image.jpg");//在d:/根目錄下放一個image.jpg的檔案
  OutputStream out = blob.getBinaryOutputStream();
  byte[] buf = new byte[10240];
  int len;
  while ((len = imgis.read(buf)) > 0) {
   out.write(buf, 0, len);
  }
  imgis.close();
  out.close();

  // 向Clob寫入實際內容
  oracle.sql.CLOB clob = (oracle.sql.CLOB) info.getResume();
  Writer writer = clob.getCharacterOutputStream();
  writer.write("this is my resume");
  writer.close();

  session.save(info);
  tx.commit();
  //讀取剛才寫入資料庫的資料
  System.out.println("讀取資料");
  DataModel info1 = (DataModel) session
    .load(DataModel.class, new Long(1));
  Clob resume = info1.getResume();
  System.out.println("resume="
    + resume.getSubString(1, (int) resume.length()));

  Blob img = info1.getImage();
  InputStream is = img.getBinaryStream();
  FileOutputStream fos = new FileOutputStream("d://outimage.jpg");//讀取到d:/outimage.jpg
  byte[] buf1 = new byte[10240];
  int len1;
  while ((len1 = is.read(buf1)) != -1) {
   fos.write(buf1, 0, len1);
  }
  fos.close();
  is.close();
  session.close();
}

右擊HibernateTest.java,將其運行為java應用程式,查詢bigdata表則會新增了一條記錄,d:/有一個名為:outimage.jpg的檔案,說明成功

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.