Java 使用spring-hibernate将blob保存到DB中
我的实体类是:Java 使用spring-hibernate将blob保存到DB中,java,spring,hibernate,session,blob,Java,Spring,Hibernate,Session,Blob,我的实体类是: @Entity @Table (name = "rs_production_dump") public class ProdDumpBLOB implements Serializable{ /** * the serial version id. */ private static final long serialVersionUID = -3395282616292841663L; @Column(name = "report_
@Entity
@Table (name = "rs_production_dump")
public class ProdDumpBLOB implements Serializable{
/**
* the serial version id.
*/
private static final long serialVersionUID = -3395282616292841663L;
@Column(name = "report_name")
protected String reportName;
@Column(name = "report")
protected Blob report;
@Column(name = "mime_type")
protected String mimeType ;
/* getters & setters*/
}
保存BLOB的DAO层方法如下:
public void saveProdDumpBLOB(ProdDumpBLOB produmpblob, FileInputStream fis, long filelength){
try {
LoggerUtil.infoApplicationLog(".... inside saveProdDumpBLOB ...." + sessionFactory);
Session session = this.sessionFactory.getCurrentSession();
Blob blob1 = Hibernate.getLobCreator(session).createBlob(fis,filelength);
produmpblob.setReport(blob1);
session.save(produmpblob);
LoggerUtil.infoApplicationLog(".... exiting saveProdDumpBLOB ....");
}
catch (Exception e) {
LoggerUtil.errorApplicationLog("Error : ProdDumpBLOBDaoImpl:saveProdDumpBLOB. #Error Message: " + e.getMessage(), e);
e.printStackTrace();
}
}
<bean id="prodDumpBlobDaoImpl" class="com.comp.ecmrs.dao.impl.ProdDumpBLOBDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
我正在尝试将excel文件保存为Blob。使用POI库创建excel文件后,已按如下方式创建并传递输入文件InputStream对象:
File xlsfile = new File(fileName);
FileInputStream fis = new FileInputStream(xlsfile);
我的hibernate-config.xml更新为会话获取映射的实体&spring-config.xml如下所示:
public void saveProdDumpBLOB(ProdDumpBLOB produmpblob, FileInputStream fis, long filelength){
try {
LoggerUtil.infoApplicationLog(".... inside saveProdDumpBLOB ...." + sessionFactory);
Session session = this.sessionFactory.getCurrentSession();
Blob blob1 = Hibernate.getLobCreator(session).createBlob(fis,filelength);
produmpblob.setReport(blob1);
session.save(produmpblob);
LoggerUtil.infoApplicationLog(".... exiting saveProdDumpBLOB ....");
}
catch (Exception e) {
LoggerUtil.errorApplicationLog("Error : ProdDumpBLOBDaoImpl:saveProdDumpBLOB. #Error Message: " + e.getMessage(), e);
e.printStackTrace();
}
}
<bean id="prodDumpBlobDaoImpl" class="com.comp.ecmrs.dao.impl.ProdDumpBLOBDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
pom.xml
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
<exclusions>
<exclusion>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.3</version>
</dependency>
公共dbcp
公共dbcp
1.2.2
共用池
共用池
com.thoughtworks.xstream
xstream
1.4.3
尝试使用支持JDBC 4的dbcp版本1.4。您使用的是哪个版本的dbcp。请共享您的maven文件HI,请查找我的pom.xml文件的片段……正如所见,它是1.2.2版本commons dbcp commons dbcp 1.2.2 commons pool commons pool com.thoughtworks.xstream xstream 1.4.3@ParulChauhan。我已根据您的评论为您的问题添加了代码。今后,请对原始帖子进行更改/编辑,而不是使用评论,以提高可读性。谢谢。我会记住的。谢谢你的帮助。切换到dbcp版本1.4纠正了abstractedbinarystream问题。然而,当更新maven依赖项时,EclipseIDE给了我一段艰难的时间