package com.fankai;import java.sql.Blob;public class Cat {
private String id;
private String name;
private char sex;
private float weight;
private Blob image;
public Cat() { } public String getId() { return id; }
public void setId(String id) { this.id = id; } public String getName() { return name; }
public void setName(String name) { this.name = name; } public char getSex() { return sex; }
public void setSex(char sex) { this.sex = sex; } public float getWeight() { return weight; }
public void setWeight(float weight) { this.weight = weight; } public Blob getImage() { return image; }
public void setImage(Blob image) { this.image = image;}
}
Cat.hbm.xml<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping>
<class name="com.fankai.Cat" table="cat">
<!--jcs-cache usage="read-only"/-->
<id name="id" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="name" length="16" not-null="true"/>
<property name="sex" length="1" not-null="true"/>
<property name="weight" />
<property name="image" />
</class>
</hibernate-mapping> 用Hibernate写入Blobpackage com.fankai;import java.sql.Blob;
import net.sf.hibernate.*;
import oracle.sql.*;
import java.io.*;public class TestCatHibernate {
public static void testBlob() {
Session s = null;
byte[] buffer = new byte[1];
buffer[0] = 1;
try {
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
s = sf.openSession();
Transaction tx = s.beginTransaction();
Cat c = new Cat();
c.setName("Robbin");
c.setImage(Hibernate.createBlob(buffer));
s.save(c);
s.flush();
s.refresh(c, LockMode.UPGRADE);
BLOB blob = (BLOB) c.getImage();
OutputStream out = blob.getBinaryOutputStream();
String fileName = "oraclejdbc.jar";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
int count = -1, total = 0;
byte[] data = new byte[(int)fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
s.flush();
tx.commit(); } catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (s != null)
try {
s.close();
} catch (Exception e) {}
}
}
}
private String id;
private String name;
private char sex;
private float weight;
private Blob image;
public Cat() { } public String getId() { return id; }
public void setId(String id) { this.id = id; } public String getName() { return name; }
public void setName(String name) { this.name = name; } public char getSex() { return sex; }
public void setSex(char sex) { this.sex = sex; } public float getWeight() { return weight; }
public void setWeight(float weight) { this.weight = weight; } public Blob getImage() { return image; }
public void setImage(Blob image) { this.image = image;}
}
Cat.hbm.xml<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping>
<class name="com.fankai.Cat" table="cat">
<!--jcs-cache usage="read-only"/-->
<id name="id" unsaved-value="null">
<generator class="uuid.hex"/>
</id>
<property name="name" length="16" not-null="true"/>
<property name="sex" length="1" not-null="true"/>
<property name="weight" />
<property name="image" />
</class>
</hibernate-mapping> 用Hibernate写入Blobpackage com.fankai;import java.sql.Blob;
import net.sf.hibernate.*;
import oracle.sql.*;
import java.io.*;public class TestCatHibernate {
public static void testBlob() {
Session s = null;
byte[] buffer = new byte[1];
buffer[0] = 1;
try {
SessionFactory sf = HibernateSessionFactory.getSessionFactory();
s = sf.openSession();
Transaction tx = s.beginTransaction();
Cat c = new Cat();
c.setName("Robbin");
c.setImage(Hibernate.createBlob(buffer));
s.save(c);
s.flush();
s.refresh(c, LockMode.UPGRADE);
BLOB blob = (BLOB) c.getImage();
OutputStream out = blob.getBinaryOutputStream();
String fileName = "oraclejdbc.jar";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
int count = -1, total = 0;
byte[] data = new byte[(int)fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
s.flush();
tx.commit(); } catch (Exception e) {
System.out.println(e.getMessage());
} finally {
if (s != null)
try {
s.close();
} catch (Exception e) {}
}
}
}
解决方案 »
- 求助:怎么使模态页面的关闭按钮“X”失效
- tomcat启动时出现的异常, 在线等求助..
- SSH运行异常
- JPA 的entity bean 为什么必须得加标识?
- 高手进入探讨
- oracle 10j 大对象blob插入有问题,急
- 我把Eclipse配制好,譬如字体大小,关键字,注释的颜色等,当我新建别的工作区后,设置全部变成原来的,怎么解决啊?
- 关于数据库存储时间问题?急~~!!!在线等
- How to lock a table row use JDBC?
- 为何大家建议不要用JDBC.ODBC而是使用微软的驱动SQL Server 2000?
- 求助
- 请问如何用JSP将XML转换成HTML?快急死人了!!!
http://www.hibernate.org.cn/viewtopic.php?t=254
http://www.hibernate.org.cn/viewtopic.php?t=13335
http://www.hibernate.org.cn/viewtopic.php?t=16392