我想用Hibernate 4.3.5.Final和Spring 4.0.6升級我們的應用程序。在我的數據庫的寫操作的應用程序的任何地方得到一個錯誤如下:如何在Spring 4.0.6中全局設置FlushMode for Hibernate 4.3.5.Final?
Caused by: 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$26.doInHibernate(HibernateTemplate.java:826)
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.deleteAll(HibernateTemplate.java:823)
...
的follwing是我的sessionFactory和transactionManager的彈簧配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/mycompany/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
1:
在爲了全局設置flushMode,以便應用程序的工作方式與以前相同,我需要將flushMode設置爲全局AUTO,所以我不想使用@Transactional(readOnly = false)方法。
2:
在下面的柱,有人建議設置singleSession爲false, Java/Hibernate - Write operations are not allowed in read-only mode
春天單證表明,指定 「singleSession」= 「假」 有副作用: http://docs.spring.io/spring/docs/4.0.6.RELEASE/javadoc-api/org/springframework/orm/hibernate3/support/OpenSessionInViewInterceptor.html
3:
我已經在web.xml中看到了很多像下面這樣的建議,它允許你截取hiberna te3會話並提供與例如會話的版本flushMode.AUTO。但是,在使用org.springframework.orm.hibernate4.support.OpenSessionInViewFilter時,這在hibernate 4中不起作用。
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
4:
提出這一方法下面是使用JPA事務管理器,它遵循一個複雜的重新實現HibernateJpaDialect的。我目前不使用JPA,這種方法似乎不夠簡單。 How do I set flush mode to "COMMIT" in my configuration files?
5:
我已經試過有在我的Spring配置如下(以下上Spring ORM 4.0.5 and Hibernate 4.3.5 - Cant save to database建議), 它似乎並沒有工作,人建議使用web.xml方法: Spring and Hibernate suddenly set the transaction to readonly
<tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
問:
任何人都可以提出一個簡單的方法來允許爲Hibernate 4.3.5.Final和Spring 4.0.6設置FlushMode嗎?
如果沒有設置清洗模式,我懷疑是錯誤的或缺少事務管理。你有'HibernateTransactionManager'這個事實並不意味着你有正確的tx設置。也只有在沒有' '的情況下添加' '才能應用它幾乎沒有用處。 –
2014-09-02 10:30:07
@ M.Deinum感謝您的評論。在我的情況下,我需要在應用程序中使用FlashMode.AUTO(這是舊版Hibernate 3.0.5的默認版本)來鏡像以前的行爲。您的回答允許我進一步調查更清潔的解決方案(如果將全局只讀設置爲全局滿足我們的應用程序的需要)。 – Max 2014-09-03 11:58:01