OpenSessionInView默认的FlushMode为
可以采用在写保存更新删除代码的时候手动更改FlushMode
但是这样做太繁琐了,第二种方式是采用spring的事务声明
代码: |
FlushMode.NEVER |
可以采用在写保存更新删除代码的时候手动更改FlushMode
代码: |
this.getHibernateTemplate().execute(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException { session.setFlushMode(FlushMode.AUTO); session.save(user); session.flush(); return null; } }); |
但是这样做太繁琐了,第二种方式是采用spring的事务声明
代码: |
<bean id="baseTransaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"/> <property name="proxyTargetClass" value="true"/> <property name="transactionAttributes"> <props> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="add*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="remove*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> |
代码: |
<bean id="userService" parent="baseTransaction"> <property name="target"> <bean class="com.phopesoft.security.service.impl.UserServiceImpl"/> </property> </bean> |