Write operations are not allowed in read-only mode - Issue while persisting

一笑奈何 提交于 2019-11-28 14:11:18

You are missing TransactionManager definition, see http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/transaction.html

[UPDATE] Previously i was writing from my mobile so it was hard to provide details, here is what you need to do:

  1. Spring xml config:

    <tx:annotation-driven/>
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    
  2. Add @Transactional annotation to CustomerDaoImpl.insertCustomer method

Now your code should work.

Please note that @Transactional annotation should be used in service layer, not in DAO layer like in this example.
@Transactional annotation tells spring to create proxy which "wraps" annotated method with transaction using aspects.

In configuration file

do the change:-

@Configuration
@EnableTransactionManagement   <-----Put this line
public PersistenceConfig{
//your code
}

(OR)

@Bean
@Autowired
public HibernateTemplate getHibernateTemplate(SessionFactory session) {
        HibernateTemplate hb = new HibernateTemplate();
        hb.setCheckWriteOperations(false);
        hb.setSessionFactory(session);
        return hb;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!