Issue
I am trying to upload a folder contain of files csv by using Spring MVC JSP.
This is my JSP file:
<form:form id="regForm" modelAttribute="patient" action="../../AddPatient/${reqUserName}/add" method="post"
enctype="multipart/form-data"
class="w3-container w3-card-4 w3-light-grey w3-margin">
<div class="w3-row w3-section">
<div class="w3-col" style="width:200px">* Input Data Files</div>
<div class="w3-rest">
<input type="file" name="fileUpload" webkitdirectory directory />
</div>
</div>
<%--button submit--%>
<form:button id="addPatient" name="addPatient" class="w3-block w3-section w3-blue w3-padding">Submit</form:button>
</form:form>
This is my controller:
@RequestMapping(value = "/AddPatient/{reqUserName}/add", method = RequestMethod.POST)
public ModelAndView saveAddPatient(@ModelAttribute("patient") Patient patient, BindingResult result,
@PathVariable String reqUserName, SessionStatus status,
HttpServletRequest request,
@RequestParam CommonsMultipartFile[] fileUpload) {
boolean error = false;
if (fileUpload != null && fileUpload.length > 0) {
for (CommonsMultipartFile aFile : fileUpload) {
System.out.println("Saving file: " + aFile.getOriginalFilename());
UploadFile uploadFile = new UploadFile();
uploadFile.setFileName(aFile.getOriginalFilename());
uploadFile.setData(aFile.getBytes());
patientService.save(uploadFile);
}
}
ModelAndView mav = new ModelAndView("success_Add_Patient", "code_module", patient.getCode_module());
mav.addObject("reqUserName", reqUserName);
return mav;
}
This is my PatientService.java:
void save(UploadFile uploadFile);
This is my PatientServiceImpl.java:
@Override
public void save(UploadFile uploadFile) {
patientDao.save(uploadFile);
}
This is the code in user-beans.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.Patient" />
<context:annotation-config />
<!--<bean id="patientService" class="com.patient.service.AssignmentServiceervice" />-->
<bean id="patientService" class="com.Patient.service.PatientServiceImpl" />
<bean id="patientDao" class="com.Patient.dao.PatientDaoImpl" >
<constructor-arg ref="hibernate5AnnotatedSessionFactory"/>
</bean>
<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate5AnnotatedSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<!--set the jdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="datasource" />
</bean>
<!--set the datasource bean-->
<bean id="datasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/db_team17" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<!--set the message source-->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="log4j" />
</bean>
</beans>
This is my table "attachment" inside my database mySQL :
This table consist of id as primary key with type bigint(20), and file_data with type blob.
And this is the error that appear once I tried to upload folder on the web that I built:
org.hibernate.HibernateException: save is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
at com.sun.proxy.$Proxy31.save(Unknown Source)
at com.Patient.dao.PatientDaoImpl.save(PatientDaoImpl.java:136)
at com.Patient.service.PatientServiceImpl.save(PatientServiceImpl.java:68)
at com.Patient.controller.PatientController.saveAddPatient(PatientController.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:870)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:881)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Anyone can help me how to solve this problem so that I can upload those file inside folder successfully into database mySQL? For additional info, I only need to upload a "folder" consist of files (CSV), and not the zip file.
Thank you
Solution
Remove hibernate.current_session_context_class
in your application configuration file or change to <property name="current_session_context_class">jta</property>
More details can be found at Error "save is not valid without active transaction
Answered By - lucumt
Answer Checked By - David Goodson (JavaFixing Volunteer)