Guest User

Untitled

a guest
Nov 27th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.13 KB | None | 0 0
  1. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
  2. org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:656)
  3. org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
  4. javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
  5. javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
  6. org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  7. Root Cause
  8.  
  9. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
  10. org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
  11. org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
  12. com.utd.dao.CustomerDaoImpl.getAllCustomer(CustomerDaoImpl.java:22)
  13. com.utd.service.CustomerManagerImpl.getAllCustomer(CustomerManagerImpl.java:23)
  14. com.utd.controller.CustomerController.listCustomers(CustomerController.java:22)
  15. sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  16. sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  17. sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  18. java.lang.reflect.Method.invoke(Method.java:498)
  19. org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
  20. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
  21. org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
  22. org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
  23.  
  24. @Controller
  25. public class CustomerController {
  26. @Autowired
  27. private CustomerManager customerManager;
  28.  
  29. @RequestMapping(value="/", method= RequestMethod.GET)
  30. public String listCustomers(ModelMap map){
  31. map.addAttribute("customer", new Customer());
  32. map.addAttribute("customerList", customerManager.getAllCustomer());
  33. return "CustomerList";
  34. }
  35.  
  36. @RequestMapping(value = "/add", method = RequestMethod.POST)
  37. public String addCustomer(@ModelAttribute(value="customer") Customer customer, BindingResult result)
  38. {
  39. customerManager.addCustomer(customer);
  40. return "redirect:/";
  41. }
  42. @RequestMapping("/delete/{customerId}")
  43. public String deleteEmplyee(@PathVariable("customerId") Integer customerId)
  44. {
  45. customerManager.deleteCustomer(customerId);
  46. return "redirect:/";
  47. }
  48.  
  49. public void setCustomerManager(CustomerManager customerManager) {
  50. this.customerManager = customerManager;
  51. }
  52. }
  53.  
  54. public class CustomerDaoImpl implements CustomerDao{
  55. @Autowired
  56.  
  57. private SessionFactory sessionFactory;
  58.  
  59. public void addCustomer(Customer customer) {
  60. this.sessionFactory.getCurrentSession().save(customer);
  61. }
  62.  
  63. @SuppressWarnings("unchecked")
  64. public List<Customer> getAllCustomer() {
  65. return this.sessionFactory.getCurrentSession().createQuery("from Customer").list();
  66. }
  67.  
  68. public void deleteCustomer(Integer customerId) {
  69. Customer customer = (Customer) sessionFactory.getCurrentSession().load(Customer.class, customerId);
  70. if (null != customer) {
  71. this.sessionFactory.getCurrentSession().delete(customer);
  72. }
  73. }
  74.  
  75. }
  76.  
  77. @Entity
  78. @Table(name="Customer")
  79. public class Customer {
  80. @Id
  81. @Column(name="ID")
  82. @GeneratedValue
  83. private int id;
  84.  
  85. @Column(name="NAME")
  86. private String name;
  87.  
  88. @Column(name="CONTACT")
  89. private String contact;
  90.  
  91. @Column(name="EMAIL")
  92. private String email;
  93.  
  94. public int getId() {
  95. return id;
  96. }
  97.  
  98. public void setId(int id) {
  99. this.id = id;
  100. }
  101.  
  102. public String getName() {
  103. return name;
  104. }
  105.  
  106. public void setName(String name) {
  107. this.name = name;
  108. }
  109.  
  110. public String getContact() {
  111. return contact;
  112. }
  113.  
  114. public void setContact(String contact) {
  115. this.contact = contact;
  116. }
  117.  
  118. public String getEmail() {
  119. return email;
  120. }
  121.  
  122. public void setEmail(String email) {
  123. this.email = email;
  124. }
  125. }
  126.  
  127. <?xml version="1.0" encoding="UTF-8"?>
  128. <beans xmlns="http://www.springframework.org/schema/beans"
  129. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  130. xmlns:aop="http://www.springframework.org/schema/aop"
  131. xmlns:context="http://www.springframework.org/schema/context"
  132. xmlns:jee="http://www.springframework.org/schema/jee"
  133. xmlns:tx="http://www.springframework.org/schema/tx"
  134. xmlns:p="http://www.springframework.org/schema/p"
  135. xmlns:util="http://www.springframework.org/schema/util"
  136. xsi:schemaLocation="http://www.springframework.org/schema/aop
  137. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  138. http://www.springframework.org/schema/beans
  139. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  140. http://www.springframework.org/schema/context
  141. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  142. http://www.springframework.org/schema/tx
  143. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  144. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
  145. http://www.springframework.org/schema/util/ http://www.springframework.org/schema/util/spring-util.xsd">
  146. <context:annotation-config />
  147. <context:component-scan base-package="com.utd.controller" />
  148. <bean id="jspViewResolver"
  149. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  150. <property name="viewClass"
  151. value="org.springframework.web.servlet.view.JstlView"></property>
  152. <property name="prefix" value="/WEB-INF/view/"></property>
  153. <property name="suffix" value=".jsp"></property>
  154. </bean>
  155. <bean id="messageSource"
  156. class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
  157. <property name="basename" value="classpath:messages"></property>
  158. <property name="defaultEncoding" value="UTF-8"></property>
  159. </bean>
  160. <bean id="propertyConfigurer"
  161. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
  162. p:location="/WEB-INF/jdbc.properties"></bean>
  163. <bean id="dataSource"
  164. class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
  165. p:driverClassName="${jdbc.driverClassName}"
  166. p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
  167. p:password="${jdbc.password}"></bean>
  168. <bean id="sessionFactory"
  169. class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  170. <property name="dataSource" ref="dataSource"></property>
  171. <property name="configLocation">
  172. <value>classpath:hibernate.cfg.xml</value>
  173. </property>
  174. <property name="configurationClass">
  175. <value>org.hibernate.cfg.AnnotationConfiguration</value>
  176. </property>
  177. <property name="hibernateProperties">
  178. <props>
  179. <prop key="hibernate.dialect">${jdbc.dialect}</prop>
  180. <prop key="hibernate.show_sql">true</prop>
  181. </props>
  182. </property>
  183. </bean>
  184. <bean id="customerDao" class="com.utd.dao.CustomerDaoImpl"></bean>
  185. <bean id="customerManager" class="com.utd.service.CustomerManagerImpl"></bean>
  186. <tx:annotation-driven />
  187. <bean id="transactionManager"
  188. class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  189. <property name="sessionFactory" ref="sessionFactory"></property>
  190. </bean>
  191. </beans>
  192.  
  193. dispatcher-servlet.xml
  194.  
  195. <?xml version="1.0" encoding="UTF-8"?>
  196. <beans xmlns="http://www.springframework.org/schema/beans"
  197. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  198. xmlns:context="http://www.springframework.org/schema/context"
  199. xmlns:mvc="http://www.springframework.org/schema/mvc"
  200. xsi:schemaLocation="http://www.springframework.org/schema/beans
  201. http://www.springframework.org/schema/beans/spring-beans.xsd
  202. http://www.springframework.org/schema/context
  203. http://www.springframework.org/schema/context/spring-context.xsd
  204. http://www.springframework.org/schema/mvc
  205. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  206. <mvc:annotation-driven />
  207. <context:component-scan base-package="com.utd" />
  208.  
  209. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  210. <property name="prefix" value="/WEB-INF/views/" />
  211. <property name="suffix" value=".jsp" />
  212. </bean>
  213.  
  214.  
  215. </beans>
Add Comment
Please, Sign In to add comment