Guest User

Untitled

a guest
Sep 7th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
  2.  
  3. <?xml version="1.0" encoding="UTF-8"?>
  4. <beans xmlns="http://www.springframework.org/schema/beans"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context.xsd
  11. ">
  12.  
  13. <import resource="hibernate.xml"/>
  14. <import resource="dataSource.xml"/>
  15.  
  16. <bean id="transactionManager"
  17. class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  18. <property name="sessionFactory" ref="sessionFactory" />
  19. </bean>
  20.  
  21.  
  22.  
  23.  
  24.  
  25. <context:component-scan base-package="neron" />
  26.  
  27. <?xml version="1.0" encoding="UTF-8"?>
  28. <beans xmlns="http://www.springframework.org/schema/beans"
  29. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  30. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  31.  
  32. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
  33. <property name="dataSource" ref="dataSource" />
  34. <property name="packagesToScan" value="neron.models" />
  35.  
  36. <property name="hibernateProperties">
  37. <props>
  38. <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
  39. <prop key="hibernate.current_session_context_class">thread</prop>
  40. <prop key="hibernate.show_sql">true</prop>
  41.  
  42. </props>
  43. </property>
  44. </bean>
  45.  
  46. <?xml version="1.0" encoding="UTF-8"?>
  47. <beans xmlns="http://www.springframework.org/schema/beans"
  48. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  49. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  50.  
  51. <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  52. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  53. <property name="url" value="jdbc:mysql://localhost:3306/test" />
  54. <property name="username" value="root" />
  55. <property name="password" value="123456789" />
  56. </bean>
  57. </beans>
  58.  
  59. package neron.dao.Impl;
  60.  
  61. import neron.dao.TestDao;
  62. import neron.models.Test;
  63. import org.hibernate.Session;
  64. import org.hibernate.SessionFactory;
  65. import org.springframework.beans.factory.annotation.Autowired;
  66. import org.springframework.stereotype.Repository;
  67.  
  68. import javax.transaction.Transactional;
  69.  
  70.  
  71. @Transactional
  72. @Repository("testDao")
  73. public class TestDaoImpl implements TestDao {
  74.  
  75.  
  76. SessionFactory sessionFactory;
  77.  
  78. @Autowired
  79. public void setSessionFactory(SessionFactory sessionFactory) {
  80. this.sessionFactory = sessionFactory;
  81. }
  82.  
  83. private Session getCurrentSession()
  84. {
  85.  
  86. return (Session)sessionFactory.getCurrentSession();
  87.  
  88. }
  89.  
  90. public void save(Test test) {
  91. getCurrentSession().save(test);
  92. }
  93.  
  94. public Test findById(int id) {
  95. return (Test) getCurrentSession().get(Test.class,id);
  96. }
  97.  
  98. Session session = getCurrentSession();
  99. Transaction trans = session.beginTransaction(); //begin transaction
  100. //db operation
  101. session.save(test)
  102. trans.commit(); //end transaction
  103.  
  104. <tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true" />
  105.  
  106. <property name="hibernate.current_session_context_class">thread</property>
Add Comment
Please, Sign In to add comment