java - Spring Hibernate Integration error -
java - Spring Hibernate Integration error -
i have been receiving next error when trying integrate spring hibernate in standalone application.
exception in thread "main" org.springframework.dao.invaliddataaccessapiusageexception: write operations not allowed in read-only mode (flushmode.manual): turn session flushmode.commit/auto or remove 'readonly' marker transaction definition. @ org.springframework.orm.hibernate4.hibernatetemplate.checkwriteoperationallowed(hibernatetemplate.java:1135) @ org.springframework.orm.hibernate4.hibernatetemplate$12.doinhibernate(hibernatetemplate.java:620) @ org.springframework.orm.hibernate4.hibernatetemplate$12.doinhibernate(hibernatetemplate.java:617) @ org.springframework.orm.hibernate4.hibernatetemplate.doexecute(hibernatetemplate.java:340) @ org.springframework.orm.hibernate4.hibernatetemplate.executewithnativesession(hibernatetemplate.java:308) @ org.springframework.orm.hibernate4.hibernatetemplate.save(hibernatetemplate.java:617) @ cardao.insert(cardao.java:38) @ cartest.main(cartest.java:19)
bean file:
public class auto { int carid; string carname; public int getcarid() { homecoming carid; } public void setcarid(int carid) { this.carid = carid; } public string getcarname() { homecoming carname; } public void setcarname(string carname) { this.carname = carname; } }
my hibernate mapping file:
<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="car" table="car"> <id name="carid" column="carid"/> <property name="carname" column="carname"/> </class> </hibernate-mapping>
cardao:
import javax.persistence.entitymanager; import org.hibernate.flushmode; import org.hibernate.session; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.annotation.scope; import org.springframework.orm.hibernate3.sessionfactoryutils; import org.springframework.orm.hibernate4.hibernatetemplate; import org.springframework.transaction.annotation.transactional; import org.springframework.transaction.support.transactiontemplate; //import org.springframework.transaction.annotation.transactional; public class cardao { hibernatetemplate template; entitymanager entitymanager; transactiontemplate template1; public hibernatetemplate gettemplate() { homecoming template; } public void settemplate(hibernatetemplate template) { this.template = template; } @transactional @scope("session") public void insert(car c){ //template.getsessionfactory().getcurrentsession().setflushmode(org.hibernate.flushmode.auto); //entitymanager.setflushmode(javax.persistence.flushmodetype.auto); // session.setflushmode(flushmode.auto); template.save(c); entitymanager.flush(); } }
applicationcontext.xml:
<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="datasourcebean" class="org.apache.commons.dbcp.basicdatasource"> <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="root"/> </bean> <bean id="sessionfactorybean" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasourcebean"/> <property name="mappingresources"> <list> <value>car.hbm.xml</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="template" class="org.springframework.orm.hibernate4.hibernatetemplate"> <property name="sessionfactory" ref="sessionfactorybean"></property> <!-- <property name="flushmodename" value="flush_commit"/> --> </bean> <bean id="cardao" class="cardao"> <property name="template" ref="template"></property> </bean> </beans>
i have searched on web appropriate solution unable find it.please help in advance
i have had error , utilize transaction solve it.
first of all, need declare hibernate4 transaction bean. write next code in applicationcontext.xml :
<bean id="transactionmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactorybean"/> </bean>
then, dao test, used straight annotation such @transactional(readonly = false)
above method need write in database (methods save, update or delete). production, set transactions in service layer aop.
some illustration : 1 of may test method utilize @transactional
:
@test @transactional(readonly = false) public void updateuser() { long userid = 1; string userfirstname = "some.firstname"; string username = "some.name"; user usertoupdate = userdao.getuser(userid); usertoupdate.setfirstname(userfirstname); usertoupdate.setfamilyname(username); userdao.updateuser(usertoupdate); user userupdated = userdao.getuser(userid); assert.assertequals(userfirstname, userupdated.getfirstname()); assert.assertequals(username, userupdated.getfamilyname()); }
if remove @transactional(readonly = false)
, test fail same error :
updateuser(project.domain.dao.hibernate.userdaohibernatetest): write operation s not allowed in read-only mode (flushmode.manual): turn session f lushmode.commit/auto or remove 'readonly' marker transaction definition.
to production now, set transaction in service layer means of aop, in spring xml configuration :
<aop:config> <aop:advisor id="managertx" advice-ref="txadvice" pointcut="execution(* *..service.manager.*manager.*(..))" order="1"/> </aop:config> <tx:advice id="txadvice"> <tx:attributes> <tx:method name="*" propagation="required" read-only="true"/> <tx:method name="create*" propagation="required" read-only="false" /> <tx:method name="save*" propagation="required" read-only="false" /> <tx:method name="update*" propagation="required" read-only="false" /> <tx:method name="delete*" propagation="required" read-only="false" /> </tx:attributes> </tx:advice>
you can configure lot of things aop, see spring doc that.
i hope help you
java spring hibernate
Comments
Post a Comment