简体   繁体   中英

Spring Hibernate Integration error

I have been receiving the following error when trying to integrate spring with hibernate in a standalone application.

Exception in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
    at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:620)
    at org.springframework.orm.hibernate4.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:617)
    at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:340)
    at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:308)
    at org.springframework.orm.hibernate4.HibernateTemplate.save(HibernateTemplate.java:617)
    at CarDAO.insert(CarDAO.java:38)
    at CarTest.main(CarTest.java:19)

Bean File:

public class Car {
    int carid;
    String carname;
    public int getCarid() {
        return carid;
    }
    public void setCarid(int carid) {
        this.carid = carid;
    }
    public String getCarname() {
        return 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() {
        return 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 over the web for appropriate solution but was unable to find it.Please help thanks in advance

I have already had this error and i use transaction to solve it.

First of all, you need to declare the hibernate4 transaction bean. Just write the following code in your ApplicationContext.xml :

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionfactoryBean"/> 
</bean>

Then, for DAO test, i used directly annotation such as @Transactional(readOnly = false) above method which need to write in database (methods like save, update or delete). To production, i set up transactions in my service layer with AOP.

Some example : this is one of may test method where i use @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 i remove the @Transactional(readOnly = false) , my test will fail with the same error as your :

UpdateUser(project.domain.dao.hibernate.UserDAOHibernateTest): Write operation
s are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into F
lushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

To production now, i set up transaction in service layer by 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 a lot of another things with AOP, see spring doc for that.

I hope this will help you

I find the answer.But I don`t know the reason.The answer are as follows.you should join some code in beans.xml.

<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>

<aop:config>
        <aop:pointcut id="bussinessService"
            expression="execution(public * com.yh.registration.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" />
    </aop:config>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="exists" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
<tx:annotation-driven/>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

If you're use spring mvc...

In dispatcher-servlet.xml

  <?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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

        <mvc:resources location="file:/C:/Users/seymur/Desktop/images/"
            mapping="/resources/**" />

        <mvc:annotation-driven />
        <tx:annotation-driven />

        <mvc:resources location="resources/" mapping="/temp/**" />
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>

        <context:component-scan base-package="scanPackages" />

    </beans>

In application-context.xml

<?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"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/webshoppingmitm" />
        <property name="username" value="root" />
        <property name="password" value="pass" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="annotatedClasses">
            <list>
                <value>packageEntity.EntityClass</value>    
            </list>
        </property>

        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            </props>
        </property>

    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <constructor-arg name="sessionFactory" ref="sessionFactory"></constructor-arg>
    </bean>



</beans>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM