感謝javaeye差沙和cac的回復.
OpenSessionInView默認的FlushMode為
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>?