开发者

Connection problems with EJB, JPA and Eclipse

开发者 https://www.devze.com 2023-04-12 20:27 出处:网络
I have made a really simple Employee class just for learning purposes. I have made a JDBC resource and connection pool in Glassfish and added the JPA Facet to my Eclipse project. I am now just trying

I have made a really simple Employee class just for learning purposes. I have made a JDBC resource and connection pool in Glassfish and added the JPA Facet to my Eclipse project. I am now just trying to generate the table from my Entity by rightcliking the Eclipse project and using JPA Tools and Generate tables from entities. And I have also added the database in eclipse and I am connected to it.

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
    <persistence-unit name="TestAreaPU">
        <jta-data-source>jdbc/testareadb</jta-data-source>
    </persistence-unit>
</persistence>

Employee Entity

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Error

[EL Warning]: PersistenceUnitInfo testareapu has transactionType RESOURCE_LOCAL and therefore jtaDataSource will be ignored
[EL Info]: EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Fine]: Detected Vendor platform: org.eclipse.persistence.platform.database.JavaDBPlatform
[EL Config]: Connection(771811155)--connecting(DatabaseLogin(   platform=>JavaDBPlatform    user name=>"APP"    datasource URL=>"jdbc:derby://localhost:1527/testareadb;create=true" ))
[EL Config]: Connection(1028854205)--Connected: jdbc:derby://localhost:1527/testareadb;create=true  User: APP   Database: Apache Derby  Version: 10.6.2.1 - (999685)    Driver: Apache Derby Network Client JDBC Driver  Version: 10.8.1.2 - (1095077)
[EL Config]: Connection(414297791)--connecting(DatabaseLogin(   platform=>JavaDBPlatform    user name=> "APP"   datasource URL=>"jdbc:derby://localhost:1527/testareadb;create=true" ))
[EL Config]: Connection(960989763)--Connected: jdbc:derby://localhost:1527/testareadb;create=true   User: APP   Database: Apache Derby  Version: 10.6.2.1 - (999685)    Driver: Apache Derby Network Client JDBC Driver  Version: 10.8.1.2 - (1095077)
[EL Info]: file:/C:/Users/Andreas/TestAreaEJB/build/classes/_testareapu_url=jdbc:derby://localhost:1527/testareadb;create=true_user=APP login successful
[EL Warning]: The collection of metamodel types is empty. Model classes may not have been found during entity search for Java SE and some Java EE container managed persistence units.  Please verify that your entity classes are referenced in persistence.xml using either <class> elements or a global <exclude-unlisted-classes>false</exclude-unlisted-classes> element
[EL Config]: Connection(1028854205)--disconnect
[EL Info]: file:/C:/Users/Andreas/TestAreaEJB/build/classes/_testareapu_url=jdbc:derby://localhost:1527/testareadb;create=true_user=APP logout successful
[EL Config]: Connection(771811155)--disconnect
[EL Config]: Connection(960989763)--disconnect

New error:

[EL Severe]: Local Exception Stack: 
Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/testareadb].
Internal Exception: javax.naming.NamingException: Lookup failed for 'jdbc/testareadb' 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.RuntimeException: Orb initialization erorr]]
    at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:485)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:109)
    at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
    at or开发者_如何转开发g.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:582)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:472)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:86)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:77)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:64)
Caused by: javax.naming.NamingException: Lookup failed for 'jdbc/testareadb' 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.RuntimeException: Orb initialization erorr]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:654)
    at javax.naming.InitialContext.lookup(InitialContext.java:396)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
    ... 11 more
Caused by: 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.RuntimeException: Orb initialization erorr]
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:352)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504)
    ... 15 more
Caused by: java.lang.RuntimeException: Orb initialization erorr
    at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:180)
    at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:365)
    at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:372)
    at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:402)
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347)
    ... 16 more
Caused by: java.lang.NullPointerException
    at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:152)
    ... 20 more

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/testareadb].
Internal Exception: javax.naming.NamingException: Lookup failed for 'jdbc/testareadb' 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.RuntimeException: Orb initialization erorr]]
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:501)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getDatabaseSession(EntityManagerFactoryDelegate.java:188)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:277)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:290)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:268)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.perform(Main.java:86)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.execute(Main.java:77)
    at org.eclipse.jpt.jpa.eclipselink.core.ddlgen.Main.main(Main.java:64)
Caused by: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [jdbc/testareadb].
Internal Exception: javax.naming.NamingException: Lookup failed for 'jdbc/testareadb' 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.RuntimeException: Orb initialization erorr]]
    at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:485)
    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:582)
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:206)
    at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:472)
    ... 7 more
Caused by: javax.naming.NamingException: Lookup failed for 'jdbc/testareadb' 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.RuntimeException: Orb initialization erorr]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:654)
    at javax.naming.InitialContext.lookup(InitialContext.java:396)
    at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:103)
    ... 11 more
Caused by: 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.RuntimeException: Orb initialization erorr]
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:352)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:504)
    ... 15 more
Caused by: java.lang.RuntimeException: Orb initialization erorr
    at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:180)
    at com.sun.enterprise.naming.impl.SerialContext.getORB(SerialContext.java:365)
    at com.sun.enterprise.naming.impl.SerialContext.getProviderCacheKey(SerialContext.java:372)
    at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:402)
    at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:347)
    ... 16 more
Caused by: java.lang.NullPointerException
    at org.glassfish.enterprise.iiop.api.GlassFishORBHelper.getORB(GlassFishORBHelper.java:152)
    ... 20 more


I think JTA is only supported in a container such as GlassFish and not from Eclipse.

Either add a persistence unit with transaction-type = "RESOURCE_LOCAL" and call this from Eclipse to create the db

or

add ";create=true" (for Apache Derby) to the connection URL in the GlassFish configuration and deploy your application.

You might also want to set the eclipselink.ddl-generation property to "create-tables" or "drop-and-create-tables"

http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_Schema_Generation


The problem seems to be in your persistence.xml, when you define the JTA data source "jdbc/testareadb" you should actually reference the Connection Pool and not the JDBC Resource.

For example, my persistence.xml is something like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
    <persistence-unit name="MyAppPU" transaction-type="JTA">
        <jta-data-source>MyAppPool</jta-data-source>
    </persistence-unit>
</persistence>

Then I need to create a JDBC Resource named "jdbc/MyAppDS" at Glassfish and a Connection Pool named "MyAppPool" that is going to be referenced by my persistence.xml. Got it?

If you don't know how to do it on Glassfish Console, take a look at this article: http://gardiary.wordpress.com/2009/07/30/create-jdbc-connection-pool-and-resource-in-glassfish/


At first, If I do entity, I would like specify table and column names:

@Entity
@Table (name = "Employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @Column (name = "firstName") //name is optional, default this is fieldName
    private String firstName;
    @Column (name = "lastName")
    private String lastName;

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

If you use <jta-data-source>jdbc/testareadb</jta-data-source>, it mean you should configure resource on server, throw glasfish admin console, where jdbc/testareadb is JNDI name of resource. If you only beginner at this, I don`t recommend do, it at first.

According to JPA 2.0 spec parameter transaction-type is optional and default is resource-local at thin client and JTA at container transactions.

Instead of jta-data-source better add properties to persistence.xml Exapmle is above:

<persistence-unit name="Unit-name" >
<class>com.yourCompany.db.Employee</class>
<properties>
      <property name="eclipselink.target-database" value="DERBY"/>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      <property name="eclipselink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
      <property name="eclipselink.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
      <property name="eclipselink.jdbc.user" value="userName"/>
      <property name="eclipselink.jdbc.password" value="userPassword"/>
    </properties>
</persistence-unit>

Good luck!


You might need to define a data source file for your environment


What you surely miss in persistence.xml (though it's not the reason for the javax.naming.NamingException) is the list of your persistence class(es) — I'm not familiar with EclipseLink, but this suggests it:

[EL Warning]: The collection of metamodel types is empty. (...)

Add the class name to your persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence (...)>
    <persistence-unit name="TestAreaPU">
        <jta-data-source>jdbc/testareadb</jta-data-source>
        <class>fully-qualified-name-of-Employee</class>
    </persistence-unit>
</persistence>
0

精彩评论

暂无评论...
验证码 换一张
取 消