hibernate+spring myeclipse小记

来源:互联网 发布:篮网数据 编辑:程序博客网 时间:2024/05/19 19:13
最近困恼了几天的东西终于搞出来了。用junit进行测试DAO类(PermissionDaoHibernateImpl),问题就一步一步清晰了。把碰到的主要问题记下:

PermissionDaoHibernateImpl代码如下:
public class PermissionDaoHibernateImpl extends HibernateDaoSupport
                                    implements PermissionDao {    
    //查询一个用户的权限,放在一个列表中,用于填充页面上的树型菜单
    public List<Permission> findUserPermission(final String userId){
      Session session = getSessionFactory().openSession();
          List<Permission> result = null;
     try{
         final String sSql =" select permission.* from permission where ID in"
            +" (select distinct permissionID from rolepermission where roleID=:uId )";
//将查询出来的一个用户的所有的权限放入一个列表中
            List<Permission> list1 = (List<Permission>) session.createSQLQuery(sSql)
                        .addEntity("permission", Permission.class)
                        .setString("uId", userId).list();
                    
             List<Permission> result1=list1;                    
                        //返回权限列表                    
                        return result1;
          }
     catch(RuntimeException ex){
                  System.out.println("chaxunquanxianchucuo"+ex);
                return result;
               }
                    
    }
}

抛出的问题:
1>junit.framework.AssertionFailedError: Exception in constructor: testFindUserPermission (org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file [F:/JSF代码/ch24/ch24/rbac/WebRoot/WEB-INF/applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/dom4j/DocumentException
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/dom4j/DocumentException
Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
        .....
由于lib下包的问题,由于有些包在WEB-INF/lib下,没有显示在referenced liberaries下。(正常情况下myeclipse中相关的到到入到WEB-INF/lib,且这个包有用,则这个包会显示在referenced liberaries下,lib下就不显示了)。解决办法:重新建立一个WEB工程,再全部导入。




2>org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean$TransactionAwareInvocationHandler.invoke(AbstractSessionFactoryBean.java:299)
    at $Proxy0.getCurrentSession(Unknown Source)
    at com.jsfabc.jsh.model.dao.hibernateImpl.PermissionDaoHibernateImpl.findUserPermission(PermissionDaoHibernateImpl.java:16)
    at com.jsfabc.jsh.model.dao.hibernateImpl.PermissionDaoImptest.testFindUserPermission(PermissionDaoImptest.java:23)
        .....
   由于开始代码中写成了 Session session = sessionFactory.getCurrentSession()。
    应该写为Session session = getSessionFactory().openSession();
    由于session是已经有了的,就不用再创建了。


     2008/7/25日
原创粉丝点击