2014-09-06 30 views
0

我想从表中获取数据我使用以下代码从数据库中获取数据。java.lang.NoClassDefFoundError:无法初始化类business.HibernateUtil

public List<UserInfoSetting> fetchAll(Long aid) { 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     Transaction tx = session.beginTransaction(); 
     List<UserInfoSetting> obj = null; 
     try { 
      String hql = "select s from UserInfoSetting s where s.atom.id=:aid "; 
      Query query = session.createQuery(hql); 
      query.setParameter("aid", aid); 
      obj = query.list(); 
      tx.commit(); 

     } catch (HibernateException e) { 
      if (tx != null) { 
       tx.rollback(); 
      } 
     } finally { 
      session.close(); 
     } 
     return obj; 
    } 

HibernateUtil.java

public class HibernateUtil { 

    private static final SessionFactory sessionFactory; 

    static { 
     try { 
       sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
     } catch (Throwable ex) { 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 

它表示以下异常

根源

java.lang.NoClassDefFoundError: Could not initialize class business.HibernateUtil 
    setting.user.UserCommunicationDao.fetchAll(UserCommunicationDao.java:146) 
    setting.user.UserCommunication.fetchAll(UserCommunication.java:64) 
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    java.lang.reflect.Method.invoke(Method.java:483) 

如何解决上述问题

+0

execption明确说明它无法找到您的类HibernateUtil – 2014-09-06 06:59:31

+0

并且还在执行select查询时为什么需要事务提交操作? ? – 2014-09-06 07:00:39

回答

0

这意味着可以为y你没有在你的类路径中获得所有正确的Hibernate库,或者你没有包括你编写的类。如果您使用Eclipse进行编码,请查找项目设置并添加Hibernate库作为项目的依赖关系。

您将需要添加hibernate-core,但也有不少其他人。

的Maven将有助于...

如果你改变了你的HibernateUtil类的名称,它会帮助你很多:有名称相同标准的Hibernate类。虽然原则上你可以有两个名字相同,但包装不同的类,但这很可能会导致混淆。 (例如,它不能完全清楚它找不到哪一个。)

相关问题