简体   繁体   中英

How can I fix JndiException on hibernate?

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

This is the class I have found on this forum to provide the session factory but I keep getting the JndiException: Error parsing JNDI name []

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

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:640) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:280) at javax.naming.InitialContext.getNameParser(InitialContext.java:429) at org.hibernate.util.NamingHelper.bind(NamingHelper.java:52) at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:90) at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:293) at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1145) at util.HibernateUtil.(HibernateUtil.java:13)

The HibernateUtil is not the problem. You should look into your configuration file .cfg.xml and remove the name on the session-factory tag.

<session-factory name=""> 

to:

<session-factory>

This is a quick fix for that problem. The name attribute is only required to bind SessionFactory to JNDI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM