ssh新手整合的历程spring+springmvc+hibernate 以及其他 一点奇葩异常的解决过程
来源:互联网 发布:js定义关联数组 编辑:程序博客网 时间:2024/05/29 19:36
★hibernate 整合 spring ,dao层注入为空,空指针异常,service层的注入没问题,
检查发现
application.xml里面没有添加对 dao包的扫描, <context:component-scan base-package="org.drill.dao" /> ,真是sb到家了。
seviceImpl里面注入dao用autowired就不要再写 get set方法了。
其实主要原因就是没扫dao包,因为dao包是自己后来才写的添加的,【配置文件是逼人给的,要求不能改动, = =!】
对hibernate的原理也不明白,主要不明白dao包里面怎么写,当时也考虑到添加扫描dao包,也尝试了添加对
dao包的扫描,但是当时还是出错了【注解注入上的错】,所以就没再考虑这个因素。
★现在的问题是,项目能启动,出现以下异常:
Servlet.service() for servlet [rest] in context with path [/drillApi] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException:Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.] with root cause
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
在网上找的解决方案:web.xml中配置的OpenSessionInViewFilter这个过滤器的时候,FlushMode就已经被默认设置为了MANUAL!
如果FlushMode是MANUAL或NEVEL,在操作过程中 hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误:
org.springframework.dao.InvalidDataAccessApiUsageException:
Write operations are not allowed in read-only mode (FlushMode.NEVER) turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition;
解决办法1:直接修改OpenSessionInViewFilter过滤器的配置,配置过滤器的时候配置就是在一般的配置里面加上下面
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.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>
但是还是不行,此时dao包里只直接写了一个类,如下
/*@Repository
public class BsxrDaoImpl extends HibernateDaoSupport {
@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
public void saveFindMess(FindMess fin) {
this.getHibernateTemplate().save(fin);
}
}*/
在前辈指导建议下,将dao层按照标准写法写了两个接口和一个实现类之后,上面的异常没有了,但是出现无法注入service的异常,
将配置文件中的对service的注入
<context:component-scan base-package="org.drill.service" />
更改为
<context:component-scan base-package="org.drill.service,org.drill.service.impl" /> ,异常解决。
这里要强调一下:以后service和serviceImpl 写在一个包里,service和serviceImpl 所在的包是service所在包的子包!!!
此时,项目正常启动,浏览器链接访问:出现:
异常:Could not obtain transaction-synchronized Session for current thread
可见是事务上的问题,添加@Transactional到serviecimpl中的@service下面,项目正常启动,浏览器链接访问:
还是异常:Could not obtain transaction-synchronized Session for current thread
之后在http://stackoverflow.com/questions/26203446/spring-hibernate-could-not-obtain-transaction-synchronized-session-for-current
发现解决方案:在daoImpl中的@Repository下面添加
@Transactional
@EnableTransactionManagement 之后所有问题解决。
但是事务是要配置在service层的,这只是权宜之计,事务相关的配置此时是写在spring-hibernate.xml中的,这里我把它转移到
application.xml里面,把daoImpl中
@Transactional
用@EnableTransactionManagement或者在配置文件里添加
<tx:annotation-driven transaction-manager="transactionManager"/>
检查发现
application.xml里面没有添加对 dao包的扫描, <context:component-scan base-package="org.drill.dao" /> ,真是sb到家了。
seviceImpl里面注入dao用autowired就不要再写 get set方法了。
其实主要原因就是没扫dao包,因为dao包是自己后来才写的添加的,【配置文件是逼人给的,要求不能改动, = =!】
对hibernate的原理也不明白,主要不明白dao包里面怎么写,当时也考虑到添加扫描dao包,也尝试了添加对
dao包的扫描,但是当时还是出错了【注解注入上的错】,所以就没再考虑这个因素。
★现在的问题是,项目能启动,出现以下异常:
Servlet.service() for servlet [rest] in context with path [/drillApi] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException:Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.] with root cause
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1135)
在网上找的解决方案:web.xml中配置的OpenSessionInViewFilter这个过滤器的时候,FlushMode就已经被默认设置为了MANUAL!
如果FlushMode是MANUAL或NEVEL,在操作过程中 hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误:
org.springframework.dao.InvalidDataAccessApiUsageException:
Write operations are not allowed in read-only mode (FlushMode.NEVER) turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition;
解决办法1:直接修改OpenSessionInViewFilter过滤器的配置,配置过滤器的时候配置就是在一般的配置里面加上下面
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>
如下:
下面是配置文件:(web.xml)<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.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>
但是还是不行,此时dao包里只直接写了一个类,如下
/*@Repository
public class BsxrDaoImpl extends HibernateDaoSupport {
@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
public void saveFindMess(FindMess fin) {
this.getHibernateTemplate().save(fin);
}
}*/
在前辈指导建议下,将dao层按照标准写法写了两个接口和一个实现类之后,上面的异常没有了,但是出现无法注入service的异常,
将配置文件中的对service的注入
<context:component-scan base-package="org.drill.service" />
更改为
<context:component-scan base-package="org.drill.service,org.drill.service.impl" /> ,异常解决。
这里要强调一下:以后service和serviceImpl 写在一个包里,service和serviceImpl 所在的包是service所在包的子包!!!
此时,项目正常启动,浏览器链接访问:出现:
异常:Could not obtain transaction-synchronized Session for current thread
可见是事务上的问题,添加@Transactional到serviecimpl中的@service下面,项目正常启动,浏览器链接访问:
还是异常:Could not obtain transaction-synchronized Session for current thread
之后在http://stackoverflow.com/questions/26203446/spring-hibernate-could-not-obtain-transaction-synchronized-session-for-current
发现解决方案:在daoImpl中的@Repository下面添加
@Transactional
@EnableTransactionManagement 之后所有问题解决。
但是事务是要配置在service层的,这只是权宜之计,事务相关的配置此时是写在spring-hibernate.xml中的,这里我把它转移到
application.xml里面,把daoImpl中
@Transactional
@EnableTransactionManagement 放到serviceImpl中,一切可以运行。
【说好的不让改配置文件呢???】
异常:Could not obtain transaction-synchronized Session for current thread主要是因为没有配置启用注解式的说明,用@EnableTransactionManagement或者在配置文件里添加
<tx:annotation-driven transaction-manager="transactionManager"/>
来解决。
总结:自己掌握的不熟练所以漏洞百出,这也是第一次接触hibernate,一路坎坷,异常不断。还有也是自己学得太死,不灵活,在此记录一下经过,加深印象。
这一段各种奇葩异常都让我碰见了,网上也几乎找不到解决办法。在此再列出几个
①eclipse只要启动maven工程,tomcat就崩溃,起不来,异常报的大意是说卡特琳娜发生错误一类的,网上各种办法都试了,困扰三天,后来把maven仓库的位置重新换个地方,重新下载jar包,问题得到解决。
②创建maven web工程时,all catlog 选项为空白,啥都没有,没法选择创建,参见这篇博客,给了我解决办法,在此向博主表示感谢。 http://blog.csdn.net/zhang5476499/article/details/51009022#comments 创建后若没有src/main/java 初始目录,检查JRE版本。
0 0
- ssh新手整合的历程spring+springmvc+hibernate 以及其他 一点奇葩异常的解决过程
- ssh(spring springMVC hibernate)的整合
- 简单的ssh(spring+springmvc+hibernate)整合
- ssh整合系列博客——(1)spring和hibernate整合的异常解决
- ssh整合系列博客——(2)spring和hibernate整合的异常解决
- Spring+SpringMVC+Hibernate三大框架整合的SSH
- SSH框架:Spring和hibernate整合时出现的异常
- SpringMVC+Hibernate+Spring的整合
- spring、springmvc、hibernate的整合
- ssh整合:spring与hibernate的整合
- SSH spring+springmvc+hibernate+maven 整合详解
- ssh之hibernate+spring的整合
- ssh-spring与hibernate的整合
- SpringMVC+Spring+Hibernate 简单的整合
- spring+springmvc+hibernate 整合出现的问题
- 我的笔记 Spring+SpringMVC+hibernate整合
- Spring+SpringMVC+Hibernate+Maven的整合
- spring springmvc hibernate(ssh)项目整合开发---邮件通信的应用
- Linux C——exec函数族
- 在springboot项目中使用jsp试图
- java中instanceof用法
- iOS开发面试知识点总结
- linux笔记-压缩及归档
- ssh新手整合的历程spring+springmvc+hibernate 以及其他 一点奇葩异常的解决过程
- 数据结构看书笔记(五)--串
- 欢迎使用CSDN-markdown编辑器
- SLIC原理简介及VLFeat代码示例
- Greenplum中内存设置不合理导致的报错
- CSS3小技巧之画图
- linux笔记-bash脚本编程(中)
- 使用者与群组
- 【PS】利用中性色绘制高亮光效果