3、整合SSH遇到的问题

遇到的问题有(只说我的解决方案,因为每个人出错原因不一样导致报相同的错): 

注:所用的版本号如下:

spring-framework-3.2.4.RELEASE-dist.zip

hibernate-release-4.2.8.Final.zip

struts-2.3.8-all.zip


异常:org.hibernate.HibernateException: No Session found for current thread

描述:由于给DAO的实现类注入了sessionFactory,想要通过sessionFactory.getCurrentSession().persist(user);方法持久化数据,而报错;但是通过     Session s = sessionFactory.openSession();  s.persist(user);是可以的;

解决方案:在UserDao接口添加 @Transactional(propagation = Propagation.REQUIRED, readOnly = false)  即可,因为未对DAO层添加事务;  

扩展知识:①其实应该在service层开启事务即可的,因为一般DAO层是不会开启事务的,此处只是为了测试;②集成hibernate4之后,最小事务级别必须是Required,如果是以下的级别,或者没有开启事务的话,无法得到当前的Session;③sessionFactory改成org.springframework.orm.hibernate4.LocalSessionFactoryBean,如果ORM映射采用的不是配置文件,是用注解的话,以前hibernate3有一个AnnotationSessionFactoryBean,在hibernate4里没看到;

org.hibernate.hql.internal.ast.QuerySyntaxException: user is not mapped [from user u where u.name=:name]

​描述:想要条件查询,按姓名查出对象报错;

解决方案:由于在配置User.hbm.xml文件时没有配置User的table名词,所以查询时表名默认是类的名称,并且大小写区分的,所以user改成User,大写即可;






原文地址:https://www.cnblogs.com/zmpandzmp/p/3648877.html