您的位置:首页 > 理论基础 > 计算机网络

SSH整合之Hibernate4遇见Spring3——Http500org.springframework.orm.hibernate4.SessionHolder cannot be cast to

2017-03-11 17:06 453 查看
问题:
今天尝试整对Struts2+Spring+Hibernate三大框架进行整合时,发现了下面这个错误
HTTP Status 500 - org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder

type Exception report
message org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.ClassCastException: org.springframework.orm.hibernate4.SessionHolder cannot be cast to org.springframework.orm.hibernate3.SessionHolder
org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:289)
org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:457)
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:393)
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:683)


原因:Hibernate 4与Spring 3有冲突
          
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
* 商品管理的DAO类
* @author ZSQ
*
*/
public class ProductDao extends HibernateDaoSupport{
/**
* DAO中保存商品的save方法
*/
public void save(Product product){
System.out.println("Dao中的save方法执行了");

//this.getHibernateTemplate().save(product);  //此为hibernate 3的hibernate模板保存方法,
//hibernate 4已经抛弃了,改成下面的方式
Session session = getSessionFactory().getCurrentSession();
session.save(product);
}
}


/*
* 分页查询用户
*/
@SuppressWarnings("unchecked")
public List<User> findByPage(int begin, int pageSize) {
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
Session session = getSessionFactory().getCurrentSession();
List<User> list = (List<User>) criteria.getExecutableCriteria(session)
.setFirstResult(begin).setMaxResults(pageSize).list();
//Hibernate4抛弃以下用法了,所以下面这行代码改为上面三行的代码
// List<User> list =
// this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
return list;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: