从实体生成表

时间:2013-11-04 15:58:17

标签: java eclipse jpa glassfish eclipselink

我正在尝试使用Project右键单击 - >从Eclipse Juno中的实体生成表格。 JPA工具 - >从实体生成表

我使用EclipseLink作为JPA Provider 我的app容器是Glassfish 4 我的数据库是在glassfish,derby中嵌入的数据库。

我的应用程序启动没有错误,我可以使用一个简单的测试bean将数据提交到数据库(所以我假设我的连接池和数据源按预期工作)

当我尝试从实体生成表时,我收到以下堆栈跟踪错误:

[EL Info]: EclipseLink, version: Eclipsenter code heree Persistence Services - 2.4.0.v20120608-r11652
[EL Severe]: ejb: Local Exception Stack: 
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: 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 org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:502)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:85)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:76)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:63)
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(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
    ... 11 more

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: 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 org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:602)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:186)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:278)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:282)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:85)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:76)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:63)
Caused by: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/__planificateur].
Internal Exception: 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 org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:502)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:685)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:213)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:542)
    ... 7 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(Unknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
    ... 11 more

这是我的persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="planificateurepicerie" transaction-type="JTA">
       <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
       <jta-data-source>jdbc/__planificateur</jta-data-source>
       <class>com.whybe.pe.jpa.Ingredient</class>
       <class>com.whybe.pe.jpa.Recette</class>
       <class>com.whybe.pe.jpa.ComposantRecette</class>
       <properties>
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
           <property name="eclipselink.logging.level" value="FINE"/>
       </properties>
    </persistence-unit>
</persistence> 

我还在Database Development透视图中设置了数据库,并且工作正常。

在glassfish控制台内部,我可以成功ping通连接池。

我的ddl无法生成的原因已经不多了......

如果您需要更多详情,请与我们联系!

提前致谢!

1 个答案:

答案 0 :(得分:1)

您在运行模式下使用transaction-type="JTA"作为独立应用程序。 JPA在默认上下文中查找JNDI资源名称(等待,当前应用程序中没有上下文,因为是运行模式独立应用程序)。对于该运行模式,请更改为transaction-type="LOCAL"。 e.g:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="SamplePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/sample"/>
      <property name="javax.persistence.jdbc.password" value="123"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="eclipselink.logging.level" value="ALL"/>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

但您正在使用Glassfish 4.在服务器上运行该应用程序。如果您没有JDNI资源名称,则需要将其添加到JDBC资源中。使用Glassfish的控制台管理更简单的方法。默认情况下,您可以在http://localhost:4848中找到它。请参阅Chapter 3 JDBC Resources

了解详情