感谢javaeye差沙和cac的回复.
OpenSessionInView默认的FlushMode为
FlushMode.NEVER
可以采用在写保存更新删除代码的时候手动更改FlushMode
但是这样做太繁琐了,第二种方式是采用spring的事务声明
可以采用在写保存更新删除代码的时候手动更改FlushMode
this
.getHibernateTemplate().execute(
new
HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
session.setFlushMode(FlushMode.AUTO);
session.save(user);
session.flush();
return null ;
}
});
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 >
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 >
< property name ="target" >
< bean class ="com.phopesoft.security.service.impl.UserServiceImpl" />
</ property >
</ bean >