Hibernate出现javax.naming.NoInitialContextException 错误的解决办法

时间:2023-09-23 11:26:20

异常信息:

08:02:56,329  WARN SessionFactoryObjectFactory:123 - Could not unbind factory from 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:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.unbind(InitialContext.java:416)
at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance(SessionFactoryObjectFactory.java:116)
at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:806)
at org.hibernate.test.junit.UserTest.tearDown(UserTest.java:43)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

很奇怪我的项目并没有使用到JNDI,但是却抛出JNDI相关的异常信息。

原来是Hibernate的配置文件hibernate.cfg.xml有问题

<hibernate-configuration>
<session-factory name="foo">
<property name="hibernate.connection.url">jdbc:oracle:thin:@10.240.144.25:1521:MES</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.username">*****</property>
<property name="hibernate.connection.password">*****</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property> <mapping resource="org/hibernate/test/User.hbm.xml"/> </session-factory>
</hibernate-configuration>

原因:<seesion-factory>元素多了属性 name.

这样,hibernate会试图把这个sessionfacotry注册到JNDI中去

解决:去了name属性