日期:2014-05-16  浏览次数:20353 次

Hibernate将图片以Blob类型存入Oracle数据库

实体:

写道
clas Object
{
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "tp", columnDefinition = "BLOB")
private Blob Picture;

public Blob getPicFile()
{
return picFile;
}

public void setPicFile(Blob picFile) {
this.picFile = picFile;
}
}

?dao:

public void saveObject(CarInfo o) {
		Session session = this.getSessionFactory().openSession();
		try{
			 session.save(o);
			 session.flush();
			 session.close();
			
		}catch(Exception e){
			if(session != null){
				session.close();
			}
		} finally {
			if(session.isOpen()){
				session.close();
			}
		}
	}

?

service:

public void saveObject(CarInfo o) {
		this.carInfoDao.saveObject(o);
	}

?页面导入图片的bean方法

public void test(UploadEvent event){
		UploadItem item = event.getUploadItem();
		try {
			FileInputStream fis = new FileInputStream(item.getFile());//定义文件读入流
			ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); 
			Blob blob = Hibernate.createBlob(fis);
			carInfo.setPicFile(blob);
			fis.close();
			bytestream.close(); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

?

bean的保存对象的方法

public String addSave(){
   this.carInfoManager.saveObject(carInfo);
}

?