• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA2, Glassfish 4, Eclipse, cannot "Generate tables from entities..." out of eclipse

 
Michael Oberleitner
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I'm new to JPA and I'm trying to get to run this simple project on glassfish 4, but it fails, when I want to create the tables from entities out of eclipse.
It looks like glassfish has troube to resolve the JNDI-name, but created a JDBC connectionpool and a associated JDBC resource. Before I used the transaction type RESOURCE_LOCAL, where everything was ok, but now i want to use managed transsactions.
The error message:

java.lang.NullPointerException
at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:347)
at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:354)
at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:384)
at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:329)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:477)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:622)
at javax.naming.InitialContext.lookup(InitialContext.java:421)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:115)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:207)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:760)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:731)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.generate(Main.java:90)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:81)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:68)
[EL Severe]: ejb: Local Exception Stack:
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.6.3.v20160218-180e602): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/MySQLDataSource].
Internal Exception: javax.naming.NamingException: Lookup failed for 'jdbc/MySQLDataSource' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.lang.NullPointerException]]
at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:523)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:121)
at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.setOrDetectDatasource(DatabaseSessionImpl.java:207)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:760)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:265)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:731)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.generate(Main.java:90)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:81)
at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:68)
Caused by: javax.naming.NamingException: Lookup failed for 'jdbc/MySQLDataSource' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.lang.NullPointerException]]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:622)
at javax.naming.InitialContext.lookup(InitialContext.java:421)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:115)
... 12 more


the persistence.xml looks like this.


This is the entity.


I hope someone here can help me, with what goes wrong with JNDI!
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Oberleitner wrote:I hope someone here can help me, with what goes wrong with JNDI!

And how does the configuration of the JNDI datasource looks like? Because that's the one you are trying to use in the persistence.xml but you didn't have posted the datasource configuration.
 
Michael Oberleitner
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Roel De Nijs,

thanks for your reply! I configured the JNDI-Resource using the administration console under htttp://localhost:4848

I copied the mysql jar file to $glassfish_install_folder\glassfish\lib
Under Resource>JDBC>JDBC Connection Pools i created a new connection pool, called MySQLConnectionPool
In the general tab as 'Resource type' i chose javax.sql.DataSource
in the advanced tab for 'Connection Validation' i checked Required=true
'Validation Method': table
and 'table name'=DUAL
in the 'Additional Properties' tab i changed the properties
URL: jdbc:mysql://localhost:3306/chat
url: jdbc:mysql://localhost:3306/chat
Password: password
User: root

and password is the correct password for user root. Then i saved this and I'm able to ping the connection pool successful.
Then I created a JDBC-Resource under Resources>JDBC<JDBC Resources
with JNDI Name: jdbc/MySQLDataSource
and Pool Name: MySQLConnectionPool
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Oberleitner wrote:Then i saved this and I'm able to ping the connection pool successful.

So the connection pool is clearly well configured.

Did you define a resource-ref in the web.xml of your web application (or in the application server specific glassfish-web.xml)? Something similar to

Are you able to retrieve the JNDI name directly (using the InitialContext class and its lookup() method)? And did you try using the complete JNDI name (something like java:/comp/env/jdbc/MySQLDataSource)?

Hope it helps!
Kind regards,
Roel
 
Michael Oberleitner
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Roel,

thanks for your help!
I don't really understand what is happening, but I found an inconvenient solution.
Eclipse does not use the jndi datasource of glassfish for the operation 'Generate Tables from Entities'. It uses the configuration
under project right click>Properties>JPA>Connection

and I had to change in persistence.xml this line


to


so I have to switch back and forth with this line in persistence.xml, when i want to rebuild the tables in the database or run the application in Glassfish.
It seems that Eclipse wants to connect with the JNDI-resource, when it reads transaction-type="JTA" in persistence.xml.
When there are better solutions I'd be happy to know!

 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Oberleitner wrote:so I have to switch back and forth with this line in persistence.xml, when i want to rebuild the tables in the database or run the application in Glassfish.
It seems that Eclipse wants to connect with the JNDI-resource, when it reads transaction-type="JTA" in persistence.xml.
When there are better solutions I'd be happy to know!

That makes sense! The JPA-Tools will not work with resource type JTA as it's running as a standalone application and no JNDI resources are available. In this topic on the Eclipse Community Forums you'll find some more info about the issue. The workaround is very simple: don't use the JPA Tools anymore, but simply run Glassfish to generate the tables from the entities using the appopriate configuration

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic