如何在hibernate上修复JndiException?

时间:2015-01-02 06:57:59

标签: java hibernate hibernate-mapping

public class HibernateUtil {
private static SessionFactory sessionFactory = createSessionFactory();
private static ServiceRegistry serviceRegistry;

public static SessionFactory createSessionFactory() {
    Configuration configuration = new Configuration();
    configuration.configure();
    serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();
    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}

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

这是我在这个论坛上发现的提供会话工厂的课程 但我一直得到JndiException: Error parsing JNDI name []

16:07:57,659 WARN SessionFactoryObjectFactory:98 - Could not bind factory to JNDI

javax.naming.NoInitialContextException:需要在环境或系统属性或applet参数或应用程序资源文件中指定类名:java.naming.factory.initial     在javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640)     在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)     在javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280)     在javax.naming.InitialContext.getNameParser(InitialContext.java:429)     在org.hibernate.util.NamingHelper.bind(NamingHelper.java:52)     at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90)     在org.hibernate.impl.SessionFactoryImpl。(SessionFactoryImpl.java:293)     在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1145)     在util.HibernateUtil。(HibernateUtil.java:13)

1 个答案:

答案 0 :(得分:0)

HibernateUtil不是问题。您应该查看配置文件.cfg.xml并删除session-factory标记上的名称。

<session-factory name=""> 

为:

<session-factory>

这是针对该问题的快速解决方法。 name属性仅用于将SessionFactory绑定到JNDI。