Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Exception in thread "main" org.hibernate.HibernateException: save is not valid without active transaction
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context.xsd
- ">
- <import resource="hibernate.xml"/>
- <import resource="dataSource.xml"/>
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <context:component-scan base-package="neron" />
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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.xsd">
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="packagesToScan" value="neron.models" />
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
- <prop key="hibernate.current_session_context_class">thread</prop>
- <prop key="hibernate.show_sql">true</prop>
- </props>
- </property>
- </bean>
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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.xsd">
- <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/test" />
- <property name="username" value="root" />
- <property name="password" value="123456789" />
- </bean>
- </beans>
- package neron.dao.Impl;
- import neron.dao.TestDao;
- import neron.models.Test;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
- import javax.transaction.Transactional;
- @Transactional
- @Repository("testDao")
- public class TestDaoImpl implements TestDao {
- SessionFactory sessionFactory;
- @Autowired
- public void setSessionFactory(SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
- private Session getCurrentSession()
- {
- return (Session)sessionFactory.getCurrentSession();
- }
- public void save(Test test) {
- getCurrentSession().save(test);
- }
- public Test findById(int id) {
- return (Test) getCurrentSession().get(Test.class,id);
- }
- Session session = getCurrentSession();
- Transaction trans = session.beginTransaction(); //begin transaction
- //db operation
- session.save(test)
- trans.commit(); //end transaction
- <tx:annotation-driven transaction-manager="transactionManager" mode="proxy" proxy-target-class="true" />
- <property name="hibernate.current_session_context_class">thread</property>
Add Comment
Please, Sign In to add comment