Struts2:无法加载HibernateSessionFactory.getSession()

时间:2013-12-30 11:25:04

标签: java hibernate struts2 jboss

我在JBoss服务器上运行一个应用程序,它是使用struts2&休眠。 但我在LoginDAOImpl类的getEmployee方法中遇到问题。代码如下: -

@SuppressWarnings("unchecked")
public List<UserView> getEmployee(String empId) {
    Session session = null;
    List<UserView> list = null;
    try {
        System.out.println("in LoginDAOImpl getEmployee : 1 ");
        session = HibernateSessionFactory.getSession();
        System.out.println("in LoginDAOImpl getEmployee : 2 " + session);
        String str = "from UserView  where empId='" + empId + "'";
        list = session.createQuery(str).list();
        System.out.println("LoginDAOImpl.getEmployee()::" + empId);
    } catch (HibernateException e) {
        e.printStackTrace();
    } finally {
        HibernateSessionFactory.closeSession();
    }
    return list;
}

在控制台Iam得到“在LoginDAOImpl getEmployee:1”,但我没有“在LoginDAOImpl getEmployee:2”。这意味着它无法找到Hibernatesessionfactory类。但我已经在我的路径中包含了Hibernatesessionfactory。 我已经为Hibernate包含了jar: - hibernate.jar文件,休眠的注解-3.2.1.ga.jar,休眠-annotations.jar,休眠-公地annotations.jar,休眠-entitymanager.jar,休眠-有效

2 个答案:

答案 0 :(得分:0)

尝试

session = HibernateSessionFactory.openSession();

答案 1 :(得分:0)

您可以提供类似的内容:

     @SuppressWarnings("unchecked")
        public List<UserView> getEmployee(String empId) {
                private static SessionFactory factory; 
                 try{
                      factory = new Configuration().configure().buildSessionFactory();
                 }catch (Throwable ex) { 
                    System.err.println("Failed to create sessionFactory object." + ex);
                     throw new ExceptionInInitializerError(ex); 
                 }

            Session session = null;
            List<UserView> list = null;
            try {
                System.out.println("in LoginDAOImpl getEmployee : 1 ");
                session = factory.openSession();
                System.out.println("in LoginDAOImpl getEmployee : 2 " + session);
                String str = "from UserView  where empId='" + empId + "'";
                list = session.createQuery(str).list();
                System.out.println("LoginDAOImpl.getEmployee()::" + empId);
            } catch (HibernateException e) {
                e.printStackTrace();
            } finally {
                session.close();
            }
            return list;
        }

有关详细信息,请参阅此link link2