oracle数据库建表语句:
create table blobTest
(
id number(19),
filedata BLOB,
primary key(id)
);
实体类:
public class Blobtest implements Serializable {
private Long id;
private byte[] filedata;
public Long getId()
{
return id;
}
public void setId(Long id)
{
this.id = id;
}
public byte[] getFiledata() {
return filedata;
}
public void setFiledata(byte[] filedata) {
this.filedata = filedata;
}
实体类与数据表的映射文件:
<hibernate-mapping package="com.thunis.ca.dto">
<class name="Blobtest" table="BLOBTEST">
<id name="id" column="ID" type="java.lang.Long">
<generator class="sequence"/>
</id>
<property name="filedata" column="FILEDATA" type="binary" />
</class>
</hibernate-mapping>
插入:
session = HibernateSessionFactory.openSession();
Blobtest bt=new Blobtest();
bt.setFiledata(lform.getFileData().getBytes());
session.save(bt);
查询:
session = HibernateSessionFactory.openSession();
String hql="from Blobtest";
List list=session.createQuery(hql).list();
if(list !=null && list.size()>0){
Blobtest bt=(Blobtest) list.get(0);
System.out.println("ID:"+bt.getId());
byte[] b=bt.getFiledata();
System.out.println("FileData:"+new String(b,0,b.length));
}