尝试设置内存中的h2数据库。
persistence.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.0">
<persistence-unit name="HelloWorldPU" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>jdbc:h2:mem:test</jta-data-source>
<class>helloworld.Message</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"></property>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
</persistence>
我跑的课程是:
package learnHibernate;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class HelloWorldJPA {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("HelloWorldPU");
System.out.println(emf);
}
}
我收到错误:
引起:org.hibernate.engine.jndi.JndiException:解析JNDI名称时出错[jdbc:h2:mem:test] 在org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:124) 在org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:95) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98) 在org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) 在org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:242) at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:210) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66) at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:88) 在org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259) ......还有14个 引发者:javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者在applet参数中或在应用程序资源文件中指定类名:java.naming.factory.initial 在javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662) 在javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) 在javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) 在javax.naming.InitialContext.getNameParser(InitialContext.java:505) 在org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:118) ......还有24个
可能是什么原因?
我该如何解决这个问题。
答案 0 :(得分:1)
JPA规范说:
8.2.1.5 jta-data-source,non-jta-data-source
在Java EE环境中,jta-data-source和non-jta-data-source元素用于指定JNDI名称[...]
在Java SE环境中,可以使用这些元素,也可以指定数据源信息 通过其他方式 - 取决于提供者的要求。
我不清楚Java SE环境中这些元素的具体含义是什么,但显然Hibernate试图访问JNDI并自然失败。
由于您已指定其他连接属性,因此您只需设置transaction-type="RESOURCE_LOCAL"
(或完全省略它,这是Java SE的默认值),并删除<jta-data-source>
元素。
然后还包括以下属性,其值已调整为正确的数据库URL:
<!-- adjust this! -->
<property name="javax.persistence.jdbc.url" value="adjust_the_value" />