第三周作业
1、项目中关于web集成hibernate和spring中遇到的问题
答:(1)最容易导致空指针的地方:在servlet层调用service层中的方法。不能运用平时的@resource注入,要添加如下方法。获取到service bean
public void init(ServletConfig config) throws ServletException {
ApplicationContext context=ContextLoader.getCurrentWebApplicationContext();
userService=(IUserService)context.getBean("userService",IUserService.class);
}
(2)在进行添加操作时,报如下错误
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
解决方案:
<filter>
<filter-name>hibernateFilter</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>
<init-param>
<param-name>singleSession</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>