无法在Hibernate

时间:2018-01-13 08:39:25

标签: java hibernate

即使我已经更改了HibernateUtil.java文件,我仍然会收到此错误:

  

java.lang.NoClassDefFoundError:无法初始化类itd.pabwe.contactbook.entities.HibernateUtil

如何解决此错误?

这是我的HibernatUtil.java代码。

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

private static SessionFactory sessionFactory;

/*
static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        factory = new AnnotationConfiguration().configure().buildSessionFactory();
        Configuration configuration = new Configuration().configure();
        StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
        SessionFactory factory = configuration.buildSessionFactory(builder.build());

    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}
*/
public static SessionFactory getSessionFactory() {
    if(sessionFactory==null){
        //loads configuration and mappings
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();

        //build a session factory from the service registry
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }

    return sessionFactory;

}
}

这里我使用的是 Hibernate 4.3.x 。请帮帮我

1 个答案:

答案 0 :(得分:0)

看起来您的工作区中似乎存在构建问题。请参阅您的jdk和jre配置正确。