Java / Hibernate - Write operations are not allowed in read-only mode

后端 未结 11 2150
迷失自我
迷失自我 2020-11-28 11:18

I\'ve been having an annoying exception a lot lately, and after some research on Google and this forum I still haven\'t found an answer that could solve my problem.

相关标签:
11条回答
  • 2020-11-28 11:44
    1. Add @Transactional above you database operation methods

    2. Make sure you have annotation driven transation manager add <tx:annotation-driven/> above HibernateTransactionManager configuration in applicationContext.xml file

    0 讨论(0)
  • 2020-11-28 11:47

    You must forgot to add @Transactional annotation to your DAO service class/method, this will specify that your database operation will be handled by spring managed transaction.

    0 讨论(0)
  • 2020-11-28 11:50

    I just stumbled onto this too. I needed to change the flush mode in Spring's OpenSessionInViewFilter to manual, and suddenly I started getting this exception. I found out that the problems were happening in methods that were not annotated as @Transactional, so I guess that Spring implicitly treats all data access code outside such methods as read only. Annotating the method solved the problem.

    Another way is to call the setCheckWriteOperations method on the HibernateTemplate object.

    0 讨论(0)
  • 2020-11-28 11:53

    Try to use this

    this.getHibernateTemplate().setCheckWriteOperations(false);
    

    Still if you are using hibernate4 and not able to update entity/table then you need to create session and need to do flush the session i.e.

    this.getHibernateTemplate().setCheckWriteOperations(false);
    Session session = this.getHibernateTemplate().getSessionFactory().openSession();
    session.saveOrUpdate(entityName,app);
    session.flush();
    session.close();
    
    0 讨论(0)
  • 2020-11-28 11:55

    I changed the single session propery from view filter. Problem solved:

      <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
          <param-name>singleSession</param-name>
          <param-value>false</param-value>
        </init-param>
      </filter>
    
    0 讨论(0)
  • 2020-11-28 11:55

    Below piece of code is worked for me.

    hibernateTemplate = new HibernateTemplate(sessionFactory);
    hibernateTemplate.setCheckWriteOperations(false);
    
    0 讨论(0)
提交回复
热议问题