简体   繁体   English

java.lang.NoClassDefFoundError:无法初始化类business.HibernateUtil

[英]java.lang.NoClassDefFoundError: Could not initialize class business.HibernateUtil

I am trying to fetch data from table I am using following code to fetch data from db. 我正在尝试从表中获取数据,我正在使用以下代码从数据库中获取数据。

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 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;
    }
}

It is showing following exception 它显示以下异常

root cause 根本原因

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)

How to resolve the above problem 如何解决以上问题

This means either you haven't got all the right Hibernate libraries in your class path, or you're not including the classes you've written. 这意味着要么您的类路径中没有所有正确的Hibernate库,要么不包括您编写的类。 If you're coding using Eclipse, find the project settings and add the Hibernate libraries as dependencies for the project. 如果您使用Eclipse进行编码,请找到项目设置并将Hibernate库添加为项目的依赖项。

You will need to add hibernate-core , but quite a few others too. 您将需要添加hibernate-core ,但也需要添加许多其他内容。

Maven would help... Maven会帮助...

And it would help you a lot if you changed the name of your HibernateUtil class: there is a standard Hibernate class with the same name. 如果您更改HibernateUtil类的名称,这将对您有很大帮助:存在一个具有相同名称的标准Hibernate类。 Although in principle you can have two classes with the same name but in different packages, it'll be likely to cause confusion. 尽管原则上可以有两个具有相同名称的类,但使用不同的包,但这可能会引起混淆。 (For instance, it's not entirely clear which one it can't find.) (例如,尚不清楚它找不到哪一个。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嵌套的异常是java.lang.NoClassDefFoundError:无法初始化com.my.util.HibernateUtil类 - nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.my.util.HibernateUtil java.lang.NoClassDefFoundError:无法初始化类biz.tugay.dao.HibernateUtil - java.lang.NoClassDefFoundError: Could not initialize class biz.tugay.dao.HibernateUtil java.lang.NoClassDefFoundError:无法初始化类org.com.hibernate.HibernateUtil - java.lang.NoClassDefFoundError: Could not initialize class org.com.hibernate.HibernateUtil java.lang.NoClassDefFoundError:无法初始化类xxx.xxx.xxx.HibernateUtil - java.lang.NoClassDefFoundError: Could not initialize class xxx.xxx.xxx.HibernateUtil java.lang.NoClassDefFoundError:无法初始化类 - java.lang.NoClassDefFoundError: Could not initialize class java.lang.NoClassDefFoundError:无法初始化类 - java.lang.NoClassDefFoundError: Could not initialize class Spring:java.lang.NoClassDefFoundError:无法初始化类 - Spring: java.lang.NoClassDefFoundError: Could not initialize class java.lang.NoClassDefFoundError:无法初始化类 XXX - java.lang.NoClassDefFoundError: Could not initialize class XXX java.lang.NoClassDefFoundError: 无法初始化类 | 静态块 - java.lang.NoClassDefFoundError: Could not initialize class | static block java.lang.NoClassDefFoundError: 无法初始化 class - Kotlin Object - java.lang.NoClassDefFoundError: Could not initialize class - Kotlin Object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM