在MySQL中對Blob與Clob是比較簡單的,如果在Oracle DB中則複雜一些,首先您要先了解 JDBC 在Oracle 9i中的作法。
由於Oracle JDBC不允許使用批次方式進行操作,所以您要在Hibernate設定檔中,將hibernate.jdbc.batch_size設定為0,例如:
- hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
....
<property name="hibernate.jdbc.batch_size">0</property>
<mapping resource="onlyfun/caterpillar/File.hbm.xml"/>
</session-factory>
</hibernate-configuration>
假設您的表格使用以下的SQL建立:
CREATE
TABLE t_file (
id INTEGER NOT NULL,
des CLOB,
image BLOB
);
id INTEGER NOT NULL,
des CLOB,
image BLOB
);
假設FILE類別設定如下:
- FILE.java
package onlyfun.caterpillar;
import java.sql.Blob;
import java.sql.Clob;
public class File {
private Integer id;
private Clob des;
private Blob image;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Clob getDes() {
return des;
}
public void setDes(Clob des) {
this.des = des;
}
public Blob getImage() {
return image;
}
public void setImage(Blob image) {
this.image = image;
}
}
映射檔案設定如下:
- FILE.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="onlyfun.caterpillar.File" table="t_file">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="des" column="des"/>
<property name="image" column="image"/>
</class>
</hibernate-mapping>
接下來Hibernate在存取Blob/Clob時,主要要模擬JDBC在Oracle中的方式,以下是個簡單的儲存Blob/Clob的方式:
- BlobClobDemo.java
package onlyfun.caterpillar;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class BlobClobDemo {
public static void main(String[] args) throws IOException, SQLException {
// Configuration 負責管理 Hibernate 配置訊息
Configuration config = new Configuration().configure();
// 根據 config 建立 SessionFactory
// SessionFactory 將用於建立 Session
SessionFactory sessionFactory = config.buildSessionFactory();
File file = new File();
// 先建立空的欄位
file.setDes(Hibernate.createClob(" "));
file.setImage(Hibernate.createBlob(new byte[1]));
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(file);
// 執行flush,讓Hibernate INSERT 空欄位
session.flush();
// 執行refresh,讓Hibernate執行SELECT FOR UPDATE
session.refresh(file, LockMode.UPGRADE);
FileInputStream fileInputStream =
new FileInputStream("c:\\workspace\\Wind.bmp");
// 重新設定真正要寫入的Blob/Clob資料
file.setDes(Hibernate.createClob("...blah...blah..."));
file.setImage(Hibernate.createBlob(fileInputStream));
// 再次save
session.save(file);
tx.commit();
session.close();
fileInputStream.close();
sessionFactory.close();
}
}
更多其它在Oracle中的解決方案,您可以參考 Using Clobs/Blobs with Oracle and Hibernate