Hibernate持久化图片问题

J2EE 码拜 9年前 (2015-04-05) 877次浏览 0个评论

File file = new File(“F:\test.jpg”);
fis = new FileInputStream(file);
byte[] bf = new byte[(int) file.length()];
String name = file.getName();
//为对象的属性赋值
TestFile testFile = new TestFile();
testFile.setName(name);//文件名
testFile.setFile(bf);//保存图片的二进制数组
session = HibernateUtil.getSessionFactory().openSession();

tx = session.beginTransaction();
session.save(testFile);
tx.commit();

这是映射文件里的配置<property name=”file” type=”binary”></property>
TestFile类的属性:private Integer id;
private String name;
private byte[] file;

不知道是哪里的问题,我每次运行都报错:
    org.hibernate.exception.DataException: could not insert: [com.aaa.photoTest.TestFile],这是什么原因?

Hibernate持久化图片问题
完整的映射配置:
<id name=”id”>
<generator class=”native”></generator>
</id>
<property name=”name” type=”string”></property>
<property name=”file” type=”binary”></property>
Hibernate持久化图片问题
40分
一般不把图片存到数据库,而是将图片路径放在数据库
Hibernate持久化图片问题
2楼正解,你每次存取图片这样数据库的要多大压力,要是高清图片呢,把图片放在一个地方,每次存取只要图片的路径就OK,这样数据库压力小,速度也会快一些!
Hibernate持久化图片问题
引用 3 楼 xn0418 的回复:

2楼正解,你每次存取图片这样数据库的要多大压力,要是高清图片呢,把图片放在一个地方,每次存取只要图片的路径就OK,这样数据库压力小,速度也会快一些!

正解+1


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Hibernate持久化图片问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!