關於Blob、Clob,可以先參考一下 將 檔案存入資料庫 這篇文章。
在Hibernate中,您可以直接對Blob、Clob作映射,例如在MySQL中,您的資料庫表格若是這麼建立的:
CREATE
TABLE user (
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default '',
age INT,
photo BLOB,
resume TEXT
);
id INT(11) NOT NULL auto_increment PRIMARY KEY,
name VARCHAR(100) NOT NULL default '',
age INT,
photo BLOB,
resume TEXT
);
您可以定義一個User類別,並讓屬性包括java.sql.Blob與java.sql.Clob,如下:
- User.java
package onlyfun.caterpillar;
import java.sql.Blob;
import java.sql.Clob;
public class User {
private Integer id;
private String name;
private Integer age;
private Blob photo;
private Clob resume;
// 必須要有一個預設的建構方法
// 以使得Hibernate可以使用Constructor.newInstance()建立物件
public User() {}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Blob getPhoto() {
return photo;
}
public void setPhoto(Blob photo) {
this.photo = photo;
}
public Clob getResume() {
return resume;
}
public void setResume(Clob resume) {
this.resume = resume;
}
}
接著在映射文件中,可以如下定義:
- User.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.User" table="user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name"/>
<property name="age" column="age"/>
<property name="photo" column="photo"/>
<property name="resume" column="resume"/>
</class>
</hibernate-mapping>
在進行資料儲存時,可以使用Hibernate.createBlob()與Hibernate.createClob()從來源資料建立Blob與 Clob實例,例如:
FileInputStream fileInputStream =
new FileInputStream("c:\workspace\photo.jpg");
Blob photo = Hibernate.createBlob(fileInputStream);
Clob resume = Hibernate.createClob("Bla....Bla....resume text!!");
User user = new User();
user.setName("caterpillar");
user.setAge(new Integer(30));
user.setPhoto(photo);
user.setResume(resume);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
Blob photo = Hibernate.createBlob(fileInputStream);
Clob resume = Hibernate.createClob("Bla....Bla....resume text!!");
User user = new User();
user.setName("caterpillar");
user.setAge(new Integer(30));
user.setPhoto(photo);
user.setResume(resume);
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(user);
tx.commit();
session.close();
如果打算從資料庫中取得資料,則一個範例如下所示:
Session
session = sessionFactory.openSession();
User user = (User) session.load(User.class, new Integer(1));
System.out.print(user.getAge() + "\t" +
user.getName() + "\t");
String resume = user.getResume().getSubString(1, (int) user.getResume().length());
System.out.println(resume);
// 將Blob資料寫到檔案
InputStream inputStream = user.getPhoto().getBinaryStream();
FileOutputStream fileOutputStream = new FileOutputStream("c:\workspace\photo_save.jpg");
byte[] buf = new byte[1];
int len = 0;
while((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
inputStream.close();
fileOutputStream.close();
System.out.println("save photo to c:\\workspace\\photo_save.jpg");
session.close();
User user = (User) session.load(User.class, new Integer(1));
System.out.print(user.getAge() + "\t" +
user.getName() + "\t");
String resume = user.getResume().getSubString(1, (int) user.getResume().length());
System.out.println(resume);
// 將Blob資料寫到檔案
InputStream inputStream = user.getPhoto().getBinaryStream();
FileOutputStream fileOutputStream = new FileOutputStream("c:\workspace\photo_save.jpg");
byte[] buf = new byte[1];
int len = 0;
while((len = inputStream.read(buf)) != -1) {
fileOutputStream.write(buf, 0, len);
}
inputStream.close();
fileOutputStream.close();
System.out.println("save photo to c:\\workspace\\photo_save.jpg");
session.close();