org.hibernate.engine.jndi.JndiException:Error parsing JNDI name [Xxxx]

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

错误信息:

org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [hibernate]

at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:124)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.bind(JndiServiceImpl.java:140)
at org.hibernate.internal.SessionFactoryRegistry.addSessionFactory(SessionFactoryRegistry.java:88)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:384)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:469)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.atguigu.hibernate.entities.MainTest.init(MainTest.java:24)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

***Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial ***

at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:685)
at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
at java.naming/javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:342)
at java.naming/javax.naming.InitialContext.getNameParser(InitialContext.java:497)
at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:118)
... 31 more

很奇怪我的项目并没有使用到JNDI,但是却抛出JNDI相关的异常信息。
原来是Hibernate的配置文件hibernate.cfg.xml有问题
下面hibernate.cfg.xml配置

<hibernate-configuration>
 <session-factory name="hibernate">
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.connection.password">12345678</property>
  <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/Spring</property>
  <!-- Hibernate的基本配置 
 使用的数据库方言 -->
  <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  <!-- 运行时是否打印SQL -->
  <property name="hibernate.show_sql">true</property>
  <!-- 运行时是否格式化SQL -->
  <property name="hibernate.format_sql">true</property>
  <!-- -->
  <property name="hibernate.use_sql_comments">true</property>
  <!-- 生成数据表的策略 -->
  <property name="hibernate.hbm2ddl.auto">update</property>
  <!-- 需要关联的hibernate映射文件   映射文件.hbm.xml -->
  <mapping resource="com/atguigu/hibernate/entities/New.hbm.xml"/>
 </session-factory
</hibernate-configuration>

原因:元素多了属性 name.
这样,hibernate会试图把这个sessionfacotry注册到JNDI中去
把这个name属性去掉即可解决该问题

你可能感兴趣的:(hibernate框架,hibernate)