在一个简单的测试方法里面在执行这个语句的时候,new Configuration().configure().buildSessionFactory();
出现javax.naming.NoInitialContextException:异常信息,请问是怎么回事?
具体Log信息如下:
2006-02-10 09:39:20,125 [main] [SessionFactoryObjectFactory] [INFO] - Factory name: eheshnew
2006-02-10 09:39:20,140 [main] [NamingHelper] [INFO] - JNDI InitialContext properties:{}
2006-02-10 09:39:20,140 [main] [NamingHelper] [DEBUG] - binding: eheshnew
2006-02-10 09:39:20,171 [main] [SessionFactoryObjectFactory] [WARN] - 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.<init>(SessionFactoryImpl.java:290)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at com.eheshnew.DBTest.main(DBTest.java:29)
------解决方法--------------------------------------------------------
应该是jndi没有配置好
------解决方法--------------------------------------------------------
配置文件名是不是hibernate.cfg.xml?
代码new Configuration().configure().buildSessionFactory();是指读入默认名为hibernate.cfg.xml的配置文件。
------解决方法--------------------------------------------------------
把
<session-factory name="eheshnew">
改成
<session-factory >
name="xxxx" 是为了把SessionFactory的唯一实例bind到jndi上的,这样可以通过new InitialContext().lookup("xxxx")来得到SessionFactory,不过实际上没什么用处。
非j2ee容器下直接执行或容器不支持jndi动态bind(如tomcat)都会出现这个问题。