Photo.java
import java.sql.Blob;
import java.sql.Clob;
import java.util.Date;
/**
* @author IBM 这是一个用于测试Hibernate大对象操作的PO
*
* 这是一个测试POJO, 属性有多种类型
*/
public class Photo {
private Long id;
private String fileName="";// 文件名
private Date date = new Date(); // 主库时间,默认值是当前时间
private Blob photo;// 相片的二进制形式,大对象
private Clob text;// 文字说明,大对象
private byte[] bin;//字节数组
}
Photo.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 package="test.com.base">
<class name="Photo" table="test_Photo">
<id name="id">
<generator class="native"/>
</id>
<property name="fileName" />
<property name="date" />
<property name="photo" />
<property name="text" />
<property name="bin" />
</class>
</hibernate-mapping>
PhotoManagerImpl.java
其中继承了BaseDao类,这是一个通用Hibernate 泛型DAO工具类
package test.com.base;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.CharBuffer;
import java.sql.Clob;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.base.dao.BaseDao;
@Transactional
@Service
public class PhotoManagerImpl extends BaseDao<Photo,Long> {
//空
}
BaseDaoBlobClobTest.java
其中用到了JUnitBase_svc类,作用是加载spring的配置文件
package test.com.base;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.sql.Blob;
import java.sql.Clob;
import javax.annotation.Resource;
import org.hibernate.Hibernate;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.base.dao.BaseDao;
/**
* 通过个单元测试,演示了<br/>
* 1: 如何操作大象<br/>
* 2:懒加载的一种解决方案<br/>
*/
public class BaseDaoBlobClobTest extends JUnitBase_svc {
@Resource
private PhotoManagerImpl photoManagerImpl;
/**
* 测试保存大对象
*/
@Test
public void TestSaveBlob() {
// ----------456.jpg 转成Blob--------------------
InputStream in = this.getClass().getResourceAsStream("phtot.jpg");
Blob blob = null;
try {
blob = Hibernate.createBlob(in);
} catch (IOException e) {
e.printStackTrace();
}
// ----------文字 转成Clob--------------------
Clob clob = Hibernate.createClob("文字 转成Clob");
// ------------------------------------------
Photo photo = new Photo();
photo.setFileName("haha.jpg");
photo.setPhoto(blob);
photo.setText(clob);
photoManagerImpl.save(photo);
}
/**
* 测试取得大对象,没有使用懒加载,
*/
@Test
public void TestGetBlob() {
Photo photo = photoManagerImpl.get(3L);// 执行Hibername的get方法,会马上执行SQL查询
int len = 0;
byte[] buf = new byte[1024];
try {
String path = "d:\\abc.jpg";// 取出图片后保存的位置
InputStream in = photo.getPhoto().getBinaryStream();
OutputStream out = new FileOutputStream(new File(path));
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
System.out.println("读出的图片放在了:" + path);
Clob clob = photo.getText();
Reader reader = clob.getCharacterStream();
char[] cb = new char[1024];
StringBuffer sb = new StringBuffer();
len = 0;
while ((len = reader.read(cb)) > 0) {
sb.append(cb, 0, len);
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 测试取得大对象,使用了懒加载,
*
* 并且是在事务已提交,session已关闭后才通过Hibernate代理查出的目标对象
*
* 这是懒加载的一种解决方案,很重要,特此说明
*/
@Test
public void TestLoadBlob() {
// 执行的是load方法,会延迟加载,返回Hibernate代理对象之后session已关闭
Photo photo = photoManagerImpl.load(3L);
//打开一个新Session,不能使用getCurrentSession()方法
Session session = photoManagerImpl.getNewSession();
System.out.println("Session是打开的吗:"+session.isOpen());
// 把Hibernate代理对象重新关联到一个session对象
session.lock(photo, LockMode.NONE);
//开启事务
Transaction t=session.beginTransaction();
int len = 0;
byte[] buf = new byte[1024];
try {
String path = "d:\\abc.jpg";// 取出图片后保存的位置
//这时才发出sql语句,执行查询,达到了懒加载的目的
InputStream in = photo.getPhoto().getBinaryStream();
OutputStream out = new FileOutputStream(new File(path));
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
System.out.println("读出的图片放在了:" + path);
Clob clob = photo.getText();
Reader reader = clob.getCharacterStream();
char[] cb = new char[1024];
StringBuffer sb = new StringBuffer();
len = 0;
while ((len = reader.read(cb)) > 0) {
sb.append(cb, 0, len);
}
;
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(t==session.getTransaction());
//提交事务
session.getTransaction().commit();
//关闭
session.close();
}
}