spring - org.hibernate.HibernateException: save is not valid without active transaction -
i have problem save data hibernate spring. when save data error:
exception in thread "main" org.hibernate.hibernateexception: save not valid without active transaction
https://gist.github.com/ruzievbakhtiyor/f3009dbc6a9c31090b59
spring beans config:
<?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" />
hibernate config 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 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>
datasource config 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>
testdao:
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); }
}
you need db update operation (insert, update, delete) within transaction boundary .
session session = getcurrentsession(); transaction trans = session.begintransaction(); //begin transaction //db operation session.save(test) trans.commit(); //end transaction
or annotation support, in spring config bean, add this
<tx:annotation-driven transaction-manager="transactionmanager" mode="proxy" proxy-target-class="true" />
Comments
Post a Comment