Parul Chauhan Parul Chauhan - 1 month ago 12
Java Question

saving blob into DB using spring - hibernate

My entity class is :

@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*/
}


and the DAO layer method for saving BLOB is as follows:

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();
}
}


i'm trying to save an excel file as Blob. The input fileinputstream object has been created and passed on as follows after creating an excel file using POI library:

File xlsfile = new File(fileName);
FileInputStream fis = new FileInputStream(xlsfile);


My hibernate-config.xml is updated with the entity mapped for session fetching & spring-config.xml is as follows:

<bean id="prodDumpBlobDaoImpl" class="com.comp.ecmrs.dao.impl.ProdDumpBLOBDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


i think this should do the trick however i'm still getting this error in my logs:

10:48:55.670 [main] DEBUG org.hibernate.internal.util.EntityPrinter - com.comp.ecmrs.dao.entity.ProdDumpBLOB{ report=org.hibernate.engine.jdbc.BlobProxy@465361b6, reportName=PROD-DUMP, mimeType=application/vnd.ms-excel}
10:48:55.686 [main] DEBUG org.hibernate.SQL - insert into REPORT.rs_production_dump (mime_type, report, report_name) values ( ?, ?, ?)
10:48:55.701 [main] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Initiating transaction rollback after commit exception
java.lang.AbstractMethodError: org.apache.commons.dbcp.DelegatingPreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V
at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$5$1.doBind(BlobTypeDescriptor.java:133) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$2$1.doBind(BlobTypeDescriptor.java:90) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:280) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:275) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.type.AbstractSingleColumnStandardBasicType.nullSafeSet(AbstractSingleColumnStandardBasicType.java:57) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2786) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3064) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:364) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:356) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:277) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:328) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175) ~[hibernate-core-4.2.1.Final.jar:4.2.1.Final]
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:558) ~[spring-orm-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:755) [spring-tx-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:724) [spring-tx-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:475) [spring-tx-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:270) [spring-tx-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) [spring-tx-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) [spring-aop-3.2.8.RELEASE.jar:3.2.8.RELEASE]
at com.sun.proxy.$Proxy47.saveProdDumpBLOB(Unknown Source) [na:na]
at com.comp.ecmrs.service.ReportServiceImpl.getProductionDumpReportData(ReportServiceImpl.java:417) [classes/:na]
at com.comp.ecmrs.test.ProductionDumpTest.testGetProdReportData(ProductionDumpTest.java:53) [test-classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_80-ea]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_80-ea]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_80-ea]
at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_80-ea]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) [junit.jar:na]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit.jar:na]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) [junit.jar:na]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit.jar:na]
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) [junit.jar:na]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) [junit.jar:na]
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) [junit.jar:na]
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) [junit.jar:na]
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) [junit.jar:na]
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) [junit.jar:na]
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) [junit.jar:na]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162) [spring-test-4.1.0.RELEASE.jar:4.1.0.RELEASE]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) [.cp/:na]
10:48:55.701 [main] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Rolling back Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.comp.ecmrs.dao.entity.ProdDumpBLOB#PLD_95103016]],collectionKeys=[]];ActionQueue[insertions=[EntityInsertAction[com.comp.ecmrs.dao.entity.ProdDumpBLOB#PLD_95103016]] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])]
10:48:55.701 [main] DEBUG org.hibernate.engine.transaction.spi.AbstractTransactionImpl - rolling back
10:48:55.701 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - rolled JDBC Connection
10:48:55.701 [main] DEBUG org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction - re-enabling autocommit
10:48:55.701 [main] DEBUG org.springframework.orm.hibernate4.HibernateTransactionManager - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com.comp.ecmrs.dao.entity.ProdDumpBLOB#PLD_95103016]],collectionKeys=[]];ActionQueue[insertions=[EntityInsertAction[com.comp.ecmrs.dao.entity.ProdDumpBLOB#PLD_95103016]] updates=[] deletions=[] collectionCreations=[] collectionRemovals=[] collectionUpdates=[] unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction


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>

Answer

Try using dbcp version 1.4 which has JDBC 4 support.