当前位置: 代码迷 >> J2EE >> Hibernate持久化图片有关问题
  详细解决方案

Hibernate持久化图片有关问题

热度:46   发布时间:2016-04-17 23:26:56.0
Hibernate持久化图片问题
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],这是什么原因?
------解决思路----------------------
一般不把图片存到数据库,而是将图片路径放在数据库
  相关解决方案