EJB、JPA 和 Eclipse 的连接问题
我出于学习目的制作了一个非常简单的员工课程。我在 Glassfish 中创建了 JDBC 资源和连接池,并将 JPA Facet 添加到我的 Eclipse 项目中。我现在只是尝试通过右键单击 Eclipse 项目并使用 JPA 工具和从实体生成表来从我的实体生成表。我还在eclipse中添加了数据库并连接到它。
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>
员工实体
@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;
}
}
错误
[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
新错误:
[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 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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为 JTA 仅在 GlassFish 等容器中受支持,而不是在 Eclipse 中受支持。
添加事务类型 =“RESOURCE_LOCAL”的持久性单元并从 Eclipse 调用它来创建数据库
,或者
将“;create=true”(对于 Apache Derby)添加到 GlassFish 配置中的连接 URL 并部署应用程序。
您可能还想将 eclipselink.ddl- Generation 属性设置为“create-tables”或“drop-and-create-tables”
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_Schema_Generation
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
问题似乎出在您的 persistence.xml 中,当您定义 JTA 数据源 "jdbc/testareadb" 时,您实际上应该引用连接池 > 而不是 JDBC 资源。
例如,我的 persistence.xml 是这样的:
然后我需要在 Glassfish 创建一个名为 "jdbc/MyAppDS" 的 JDBC 资源和一个名为 的连接池“MyAppPool” 将由我的 persistence.xml 引用。知道了?
如果您不知道如何在 Glassfish Console 上执行此操作,请查看这篇文章:
http://gardiary. wordpress.com/2009/07/30/create-jdbc-connection-pool-and-resource-in-glassfish/
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:
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/
首先,如果我做实体,我想指定表和列名称:
如果您使用
jdbc/testareadb
,则意味着您应该在服务器上配置资源,抛出 glasfish 管理控制台,其中 jdbc/testareadb 是资源的 JNDI 名称。如果您只是初学者,我不建议您一开始就这样做。
根据 JPA 2.0 规范,参数
transaction-type
是可选的,瘦客户端默认为resource-local
,容器事务默认为JTA
。最好将属性添加到 persistence.xml,而不是
jta-data-source
例子如上:
祝你好运!
At first, If I do entity, I would like specify table and column names:
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 isresource-local
at thin client andJTA
at container transactions.Instead of
jta-data-source
better add properties to persistence.xmlExapmle is above:
Good luck!
您可能需要为您的环境定义数据源文件
You might need to define a data source file for your environment
你肯定在
persistence.xml
中错过了你的持久性类的列表(尽管这不是javax.naming.NamingException
的原因) - 我是不熟悉 EclipseLink,但这表明了这一点:将类名添加到 persistence.xml 中:
What you surely miss in
persistence.xml
(though it's not the reason for thejavax.naming.NamingException
) is the list of your persistence class(es) — I'm not familiar with EclipseLink, but this suggests it:Add the class name to your persistence.xml: