Ohmen
Ohmen

Reputation: 6604

Generating Tables from entities - JPA

I want to gernerate a table in a Database. I use a non-embedded Derbry Database and glassfish4.0 + ecipse link.

First I configured a new JDBC connectionpool with the glassfish admin-console. The URL ist jdbc:derby://localhost:1527/firstEeDb;create=true (Derby networkserver ist Stated).

Then i created a new JDBC Resource named testDb and after the first Acces to the DB over Eclipse Database-view there is a folder named testDb in my derby folder.

I tried right-click -> JPA Tools -> Generate Tables from Entities, but it doesn't work.

I get this exception:

[EL Config]: metadata: The column name for element [id] is being defaulted to: ID.
[EL Config]: metadata: The column name for element [strasse] is being defaulted to: STRASSE.
[EL Config]: metadata: The column name for element [plz] is being defaulted to: PLZ.
[EL Info]: EclipseLink, version: Eclipse Persistence Services - 2.5.0.v20130507-3faac2b
Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/testDb].
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:762)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
    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: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/testDb].
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:520)
    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.setOrDetectDatasource(DatabaseSessionImpl.java:203)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:734)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:681)
    ... 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(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:404)
    at javax.naming.InitialContext.lookup(InitialContext.java:415)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
    ... 12 more
[EL Severe]: ejb: Local Exception Stack: 
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/testDb].
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:520)
    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.setOrDetectDatasource(DatabaseSessionImpl.java:203)
    at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:734)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:239)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:681)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:204)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:304)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:336)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:302)
    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.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:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:404)
    at javax.naming.InitialContext.lookup(InitialContext.java:415)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
    ... 12 more

Entity:

import java.io.Serializable;

import javax.inject.Named;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Named
public class Adresse implements Serializable {

    private static final long serialVersionUID = -1864720487909609602L;

    @Id
    @GeneratedValue
    private long id;

    private int plz;    
    private String strasse; 

    public Adresse() {
    }

    public Adresse(int plz, String strasse) {
        super();
        this.plz = plz;
        this.strasse = strasse;
    }

    public int getPlz() {
        return plz;
    }

    public void setPlz(int plz) {
        this.plz = plz;
    }

    public String getStrasse() {
        return strasse;
    }

    public void setStrasse(String strasse) {
        this.strasse = strasse;
    }
}

persistece.xml

    <?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="Adressbuch" transaction-type="JTA">
        <jta-data-source>jdbc/testDb</jta-data-source>
        <class>myPackage.Adresse</class>

        <properties>
            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
        </properties>

    </persistence-unit>
</persistence> 

Upvotes: 2

Views: 8842

Answers (3)

Ohmen
Ohmen

Reputation: 6604

Solution

I selected the wrong source for JPA-tools

Upvotes: 1

Ohmen
Ohmen

Reputation: 6604

As work arround i build a second persistence.xml whitch defines the database as local resouce and then right-click -> JPA Tools -> Generate Tables from Entities creates everything for me. newPersistece.xml

<persistence-unit name="Adressbuch" transaction-type="RESOURCE_LOCAL">
    <class>myPackage.Adresse</class>        
    <properties>
        <!-- EclipseLink should create the database schema automatically -->
        <property name="eclipselink.ddl-generation" value="create-tables" />
        <property name="eclipselink.ddl-generation.output-mode"
            value="database" />
        <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"></property>
        <property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/firstEeDb;create=true"></property>
        <property name="javax.persistence.jdbc.user" value="APP"></property>
        <property name="javax.persistence.jdbc.password" value="APP"></property>
    </properties>

</persistence-unit>

Upvotes: 2

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

This happens because you use the IDE to create the tables; and the IDE doesn't run in a context where the JNDI resources have been set up. Either specify the plain connection parameters in the persistence.xml, or just run your JPA application in a JEE container that has the necessary JNDI resources.

Upvotes: 0

Related Questions