编译时出现Hibernate错误

时间:2015-03-16 19:19:48

标签: eclipse hibernate

我试图在Eclipse中的动态Web项目中加入Hibernate v4。我正在使用Maven作为依赖项。我编写了所有代码并创建了实体类并映射了xml文件,但是在构建项目时我得到了这个错误

The project was not built since its build path is incomplete. Cannot find the class file for javax.naming.Referenceable. 
Fix the build path then try building this project MyProject

我写了HibernateUtil类:

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 = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try {
        if (sessionFactory == null) {
            Configuration configuration = new Configuration()
                    .configure(HibernateUtil.class
                            .getResource("/hibernate.cfg.xml"));
            StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
            serviceRegistryBuilder.applySettings(configuration
                    .getProperties());
            ServiceRegistry serviceRegistry = serviceRegistryBuilder
                    .build();
            sessionFactory = configuration
                    .buildSessionFactory(serviceRegistry);
        }
        return sessionFactory;
    } catch (Throwable ex) {
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

public static void shutdown() {
    getSessionFactory().close();
}
}

编辑:我在关机方法中调用getSessionFactory方法时遇到此错误。 谁知道会有什么问题?

1 个答案:

答案 0 :(得分:1)

问题在于您尝试在某处使用Referenceable接口类,并且该类在构建路径上不可用。就像它说的那样,你必须修复构建路径。通过提供具有javax.naming.Referenceable的包来完成此任务。

我觉得奇怪的是为什么重写HibernateUtil类?这可能不是你想要做的事情。而且我没有解释为什么你会写自己的。

我建议忘记HibernateUtil类并开始完全使用Hibernate框架。