spring+springmvc+hibernate整合遇到的问题

spring+springmvc+hibernate整合遇到的问题
2016年10月20日 23:24:03 守望dfdfdf 阅读数:702 标签: ssh学习经历的异常exception异常框架更多
个人分类: 工作 问题
编辑
版权声明:本文为博主原创文章,转载请注明文章链接。 https://blog.csdn.net/xiaoanzi123/article/details/52878325
★hibernate 整合 spring ,dao层注入为空,空指针异常,service层的注入没问题,
检查发现
application.xml里面没有添加对 dao包的扫描, <context:component-scan base-package="org.drill.dao" /> 。
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版本。

猜你喜欢

转载自www.cnblogs.com/dxxdsw/p/10705087.html
今日推荐