hibernate中映射blob数据类型的一个例子
2012-01-16 14:47
375 查看
Java 代码
publicclass User implements java.io.Serializable {
// Fields
privatelong id;
private String name;
private String email;
private String addr;
//定义Blob的pthto
private Blob photo;
xml 代码
<hibernate-mapping>
<classname="org.tie.User"table="user"catalog="tie">
<idname="id"type="long">
<columnname="id"/>
<generatorclass="identity"/>
</id>
<propertyname="name"type="string">
<columnname="name"length="45"not-null="true"/>
</property>
<propertyname="email"type="string">
<columnname="email"length="45"/>
</property>
<propertyname="addr"type="string">
<columnname="addr"length="45"/>
</property>
<!-- 映射blob类型 -->
<propertyname="photo"type="blob">
<columnname="photo"/>
</property>
</class>
</hibernate-mapping>
两个测试方法:
java 代码
publicvoid testCreate(){
User user = new User();
user.setName("linweiyang");
user.setAddr("beijing");
user.setEmail("linweiyang@163.com");
Blob photo = null;
try {
//将图片读进输入流
FileInputStream fis =new FileInputStream("c:\\a.jpg");
//转成Blob类型
photo = Hibernate.createBlob(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
user.setPhoto(photo);
Session session = factory.openSession();
Transaction tr = session.beginTransaction();
session.save(user);
tr.commit();
session.close();
}
publicvoid testRerieve(){
Session session = factory.openSession();
User user = (User)session.load(User.class,new Long(3));
try {
//从数据库中要读取出来
InputStream is = user.getPhoto().getBinaryStream();
//在把写到一个图片格式的文件里
FileOutputStream fos =new FileOutputStream("c:\\linweihan.jpg");
byte[] buffer =new
byte[1024];
int len =0;
//从数据库中读取到指定的字节数组中
while((len = is.read(buffer) )!= -1){
//从指定的数组中读取,然后输出来,所以这里buffer好象是连接inputStream和outputStream的一个东西
fos.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
session.close();
}
这么理解输入输出流
读入流,自然要有读入的源头,
输出也要输出到某个地方,输出一般是先要输读入,
这里连接输入和输出的是一个在内存中的字节数组buffer.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里.
来源:http://hi.baidu.com/networkcrazy/blog/item/1398b155f0ef1350d0090602.html
publicclass User implements java.io.Serializable {
// Fields
privatelong id;
private String name;
private String email;
private String addr;
//定义Blob的pthto
private Blob photo;
xml 代码
<hibernate-mapping>
<classname="org.tie.User"table="user"catalog="tie">
<idname="id"type="long">
<columnname="id"/>
<generatorclass="identity"/>
</id>
<propertyname="name"type="string">
<columnname="name"length="45"not-null="true"/>
</property>
<propertyname="email"type="string">
<columnname="email"length="45"/>
</property>
<propertyname="addr"type="string">
<columnname="addr"length="45"/>
</property>
<!-- 映射blob类型 -->
<propertyname="photo"type="blob">
<columnname="photo"/>
</property>
</class>
</hibernate-mapping>
两个测试方法:
java 代码
publicvoid testCreate(){
User user = new User();
user.setName("linweiyang");
user.setAddr("beijing");
user.setEmail("linweiyang@163.com");
Blob photo = null;
try {
//将图片读进输入流
FileInputStream fis =new FileInputStream("c:\\a.jpg");
//转成Blob类型
photo = Hibernate.createBlob(fis);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
user.setPhoto(photo);
Session session = factory.openSession();
Transaction tr = session.beginTransaction();
session.save(user);
tr.commit();
session.close();
}
publicvoid testRerieve(){
Session session = factory.openSession();
User user = (User)session.load(User.class,new Long(3));
try {
//从数据库中要读取出来
InputStream is = user.getPhoto().getBinaryStream();
//在把写到一个图片格式的文件里
FileOutputStream fos =new FileOutputStream("c:\\linweihan.jpg");
byte[] buffer =new
byte[1024];
int len =0;
//从数据库中读取到指定的字节数组中
while((len = is.read(buffer) )!= -1){
//从指定的数组中读取,然后输出来,所以这里buffer好象是连接inputStream和outputStream的一个东西
fos.write(buffer,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
session.close();
}
这么理解输入输出流
读入流,自然要有读入的源头,
输出也要输出到某个地方,输出一般是先要输读入,
这里连接输入和输出的是一个在内存中的字节数组buffer.这样从数据库中读到这个数组里,输出流在从这个数组中输出到特定的文件格式里.
来源:http://hi.baidu.com/networkcrazy/blog/item/1398b155f0ef1350d0090602.html
相关文章推荐
- Hibernate Blob数据类型映射的一个例子,从BLOB中取值
- Hibernate Blob数据类型映射的一个例子
- Hibernate处理一个实体映射多张相同结构的数据表--动态映射
- 使用 hibernate 存取大对象数据类型(clob和blob)
- Hibernate自定义数据类型映射oracle中xmlType 广东精鹰软件工作室(刘正仁)
- 一个简单但详细的解释Windows文件映射读取数据文件的例子
- Hibernate的基本映射数据类型
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- hibernate数据类型与OR映射
- Hibernate的基本映射数据类型--(2)
- Hibernate的基本映射数据类型
- Hibernate数据类型映射关系
- Hibernate继承映射--每个类一个数据表的实现案例
- hibernate级联操 同时向2张表插入数据(一对多的关联,一个简单的例子说明)
- hibernate中一对多映射(set集合中存放的原生数据类型,而不是实体)
- hibernate映射的数据类型!(转)
- hibernate数据类型之间的映射关系
- Java8Map示例:一个略复杂的数据映射聚合例子及代码重构
- java hibernate 映射和注解oracle含有blob字段的数据表的pojo源码
- Hibernate中对于数据的(增删改查)操作的一个例子的代码