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],这是什么原因?
------解决思路----------------------
一般不把图片存到数据库,而是将图片路径放在数据库