1

Trying to set up an in memory h2 database.

The persistence.xml is:

<?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> 

The class i run is:

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

I am getting an error:

Caused by: org.hibernate.engine.jndi.JndiException: Error parsing JNDI name [jdbc:h2:mem:test] at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:124) at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:95) at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:98) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:94) at 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) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:259) ... 14 more Caused by: 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:662) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313) at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350) at javax.naming.InitialContext.getNameParser(InitialContext.java:505) at org.hibernate.engine.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:118) ... 24 more

What could be the reason?

How can i fix this.

1 Answer 1

2

JPA spec says:

8.2.1.5 jta-data-source, non-jta-data-source

In Java EE environments, the jta-data-source and non-jta-data-source elements are used to specify the JNDI name [...]

In Java SE environments, these elements may be used or the data source information may be specified by other means—depending upon the requirements of the provider.

It is not clear to me what is the specified meaning for these elements in Java SE environments, but obviously Hibernate tries to access JNDI and naturally fails.

Since you have specified the other connection properties, you can simply set transaction-type="RESOURCE_LOCAL" (or omit it altogether, this is the default value for Java SE), and remove the <jta-data-source> element too.

Then also include the following property, its value adjusted to the correct db URL:

<!-- adjust this! --> <property name="javax.persistence.jdbc.url" value="adjust_the_value" /> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.