Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
  2.  
  3. <bean id="userDAO" class="com.my.portal.user.UserDAOImpl">
  4. <property name="sessionFactory" ref="sessionFactory"/>
  5. </bean>
  6.  
  7. <context-param>
  8. <param-name>contextConfigLocation</param-name>
  9. <param-value>/WEB-INF/spring/*.xml</param-value>
  10. </context-param>
  11.  
  12. <!-- Creates the Spring Container shared by all Servlets and Filters -->
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16.  
  17. <!-- Processes application requests -->
  18. <servlet>
  19. <servlet-name>appServlet</servlet-name>
  20. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  21. <init-param>
  22. <param-name>contextConfigLocation</param-name>
  23. <param-value>WEB-INF/spring/servlet-context.xml</param-value>
  24. </init-param>
  25. <load-on-startup>1</load-on-startup>
  26. </servlet>
  27.  
  28. <servlet-mapping>
  29. <servlet-name>appServlet</servlet-name>
  30. <url-pattern>/</url-pattern>
  31. </servlet-mapping>
  32.  
  33. <!-- Data Source Setup -->
  34. <bean id="myDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  35. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  36. <property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
  37. <property name="username" value="root"/>
  38. <property name="password" value=""/>
  39. <property name="maxIdle" value="5"/>
  40. <property name="maxActive" value="10"/>
  41. </bean>
  42.  
  43. <!-- Hibernate Sessionfactory Bean Definition -->
  44. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  45. <property name="dataSource" ref="myDS"/>
  46. <property name="annotatedClasses">
  47. <list>
  48. <value>com.my.portal.user.User</value>
  49. </list>
  50. </property>
  51. <property name="hibernateProperties">
  52. <props>
  53. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
  54. <prop key="hibernate.show_sql">true</prop>
  55. </props>
  56. </property>
  57. </bean>
  58.  
  59. <!-- Hibernate Transaction Setup -->
  60. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  61. <property name="sessionFactory" ref="sessionFactory"/>
  62. </bean>
  63.  
  64. <tx:annotation-driven transaction-manager="transactionManager"/>
  65.  
  66. @Entity
  67.  
  68. @Id
  69. @Column(name="UserId")
  70. @GeneratedValue(strategy=GenerationType.IDENTITY)
  71. private int UserId;
  72. private String FirstName;
  73. private String LastName;
  74. public int getUserId() {
  75. return UserId;
  76. }
  77. public void setUserId(int userId) {
  78. UserId = userId;
  79. }
  80. public String getFirstName() {
  81. return FirstName;
  82. }
  83. public void setFirstName(String firstName) {
  84. FirstName = firstName;
  85. }
  86. public String getLastName() {
  87. return LastName;
  88. }
  89. public void setLastName(String lastName) {
  90. LastName = lastName;
  91. }
  92. @Override
  93. public String toString(){
  94. return "UserId="+UserId+", FirstName="+FirstName+", LastName="+LastName;
  95. }
  96.  
  97. @Controller
  98.  
  99. private static final Logger logger = LoggerFactory.getLogger(UserController.class);
  100.  
  101. private UserService userService;
  102.  
  103. @Autowired
  104. public UserController(UserService userService){
  105. this.userService = userService;
  106. }
  107.  
  108. @RequestMapping(value = "/user", method = RequestMethod.GET)
  109. public String user(Locale locale, Model model){
  110. model.addAttribute("getUser",userService.getUser());
  111. return "user";
  112.  
  113. }
  114.  
  115. @Repository
  116.  
  117. public List<User> getUser();
  118.  
  119. private static final Logger logger = LoggerFactory.getLogger(UserDAOImpl.class);
  120.  
  121. private SessionFactory sessionFactory;
  122.  
  123. @Autowired
  124. public void setSessionFactory(SessionFactory sessionFactory){
  125. this.sessionFactory = sessionFactory;
  126. }
  127.  
  128. private Session currentSession() {
  129. return sessionFactory.getCurrentSession();
  130. }
  131.  
  132. @SuppressWarnings("unchecked")
  133. @Override
  134. public List<User> getUser() {
  135. List<User> userList = currentSession().createQuery("from User").list();
  136. return userList;
  137. }
  138.  
  139. @Service
  140.  
  141. public String getMessage();
  142.  
  143. @Service
  144.  
  145. private UserDAO userDAO;
  146.  
  147. @Autowired
  148. public void setUserDAO(UserDAO userDAO) {
  149. this.userDAO = userDAO;
  150. }
  151.  
  152. public String getMessage(){
  153. return "User Registration Page";
  154. }
  155.  
  156. @SuppressWarnings("unchecked")
  157. @Override
  158. @Transactional
  159. public List<User> getUser(){
  160. logger.info("getUser()");
  161. return this.userDAO.getUser();
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement