views:

427

answers:

1

I'm struggling to set connect a Java program to MySQL using JPA/Hibernate.

I'm currently getting the following error when I try to call createEntityManagerFactory():

[main] ERROR org.hibernate.connection.DatasourceConnectionProvider - Could not find datasource: java:jdbc/myDataDS
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.lookup(InitialContext.java:392)
at org.hibernate.connection.DatasourceConnectionProvider.configure(DatasourceConnectionProvider.java:75)

Googling seems to indicate that I need a jndi.properties file in META-INF in my classpath, but I can't seem to find any information about what that file should contain in my case.

Edit: I'm running this stand-alone, for the time being.

A: 

A jndi.properties file should be at the root of the classpath and typically contains the URL of the JNDI server and the initial context factory to use. For example, with JBoss:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=jnp://localhost:1099

But, when using Hibernate, you should actually declare these properties in the hibernate.cfg.xml. For example, with WebLogic:

<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="jndi.url">t3://127.0.0.1:7001</property>
Pascal Thivent
How do I determine the values for "jndi.class" and "jndi.url"?
FarmBoy
@FarmBoy They depend on your app server and its configuration. What app server are you using?
Pascal Thivent
I'm currently running this as a stand-alone application.
FarmBoy
@FarmBoy Then performing a lookup to get a datasource from JNDI is not the right approach. You should use Hibernate's stand-alone connection pool.
Pascal Thivent
I could wonder how one does that that, but I've backed off to using straight JDBC. Thanks for your efforts, but I evidently don't know enough to be helped right now.
FarmBoy