今天在学习使用hibernate4,在调用sessionFactory.getCurrentSession().save(entity);进行保存操作时,方法执行成功,但是数据没有插入到数据库,控制台也不报错,在网上各种查找解决方法之后找到思路,这个问题是跟flushMode的属性有关,为什么会这样呢?因为我在dispatcher-servlet.xml和spring-hibernate.xml中都是配置的
<context:component-scan base-package="com.test">
后来把dispatcher-servlet.xml中改为 <context:component-scan base-package="com.orange">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>
spring-hibernate.xml中改为 <context:component-scan base-package="com.orange">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
重新运行项目,数据能够insert到数据库中了。记得web.xml里配置
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我是个小菜鸟,只是把我的问题记录在这儿了,大家不喜勿喷。