我是JPA中的新成員。IBM Websphere JPA org.apache.openjpa.persistence.ArgumentException
我創建了一個JPA實體(見下文)。當我嘗試從java客戶端使用它時,當我向數據庫中插入一條記錄時,我有以下例外(請參閱下文)
任何想法可以跳過這個錯誤?
客戶:
package co.ne.staffmanagement.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import co.ne.staffmanagement.domain.Employee;
public class TestHarness {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EXAMPLES");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
Employee employee1 = new Employee ("Lucia", "Ochoa", "Manager", 10000);
em.persist(employee1);
tx.commit();
em.close();
}
}
實體:
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="EXAMPLES" transaction-type="RESOURCE_LOCAL">
<class>co.ne.staffmanagement.domain.Employee</class>
<properties>
<property name="openjpa.ConnectionURL"
value="jdbc:db2://localhost:50000/EXAMPLES:retrieveMessagesFromServerOnGetMessage=true;" />
<property name="openjpa.ConnectionDriverName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="openjpa.DynamicEnhancementAgent" value="true"/>
<property name="openjpa.ConnectionUserName" value="db2admin" />
<property name="openjpa.ConnectionPassword" value="db2admin" />
<property name="openjpa.Log" value="SQL=TRACE" />
<property name="eclipselink.logging.level" value="FINE" />
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
</properties>
</persistence-unit>
</persistence>
異常和控制檯輸出:
IBM WebSphere Application Server, Release 8.5
Java EE Application Client Tool
Copyright IBM Corp., 1997-2011
WSCL0012I: Processing command line arguments.
WSCL0001I: Command line, property file, and system property arguments resolved to:
File to launch = C:\Users\User\IBM\rationalsdp\workspace\EmployeeManagement
CC Property File = null
Client Jar File = EmployeeManagementClient.jar
Alternate DD = null
BootstrapHost = null
BootstrapPort = <default>
Trace enabled = false
Tracefile = null
Init only = false
Classpath Parameter = null
Security Manager = disable
Security Manager Class = Not used. -CCsecurityManager=disable
Security Manager Policy = Not used. -CCsecurityManager=disable
Exit VM = false
Soap Connector Port = null
Application Parameters =
Provider URL = null
Dump Java Name Space = null
Admin Connector Host = null
Admin Connector Port = null
Admin Connector Type = null
Admin Connector User = null
PARENT_LAST mode = false
WSCL0013I: Initializing the Java EE Application Client Environment.
[8/31/12 10:08:51:464 COT] 00000001 W UOW=null source=com.ibm.ws.ssl.config.SSLConfig org=IBM prod=WebSphere component=Application Server thread=[P=731088:O=0:CT]
CWPKI0041W: One or more key stores are using the default password.
WSCL0600I: Binding HandleDelegate object.
WSCL0031I: The object was bound successfully.
WSCL0900I: Initializing and starting components.
WSCL0910I: Initializing component: com.ibm.ws.runtime.component.WsByteBufferPoolManagerClientServiceImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.activity.ActivityServiceClientComponentImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.runtime.component.ChannelFrameworkClientServiceImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.runtime.component.NonRecoverableComponentImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.webservices.wssecurity.core.WSSecurityClientComponentImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.runtime.component.CFSBinder
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.runtime.component.WSBBSBinder
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.cluster.channel.ChannelSelectionAdapterImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.handlerfw.impl.HandlerFrameworkServiceImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.ActivitySession.ActivitySessionClientComponentImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.appprofile.AppProfileClientComponentImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.webservices.component.WSClientImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.workarea.WorkAreaServiceClient
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.injectionengine.ClientInjectionEngineImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ws.websvcs.component.WASAxis2ClientImpl
WSCL0911I: Component initialized successfully.
WSCL0910I: Initializing component: com.ibm.ejs.jms.JMSClientRegistration
WSCL0911I: Component initialized successfully.
WSCL0901I: Component initialization completed successfully.
WSCL0035I: Initialization of the Java EE Application Client Environment has completed.
WSCL0014I: Invoking the Application Client class co.ne.staffmanagement.test.TestHarness
162 EXAMPLES INFO [P=731088:O=0:CT] openjpa.Runtime - OpenJPA dynamically loaded a validation provider.
191 EXAMPLES INFO [P=731088:O=0:CT] openjpa.Runtime - Starting OpenJPA 2.2.1-SNAPSHOT
275 EXAMPLES INFO [P=731088:O=0:CT] openjpa.jdbc.JDBC - Using dictionary class "com.ibm.ws.persistence.jdbc.sql.DB2Dictionary".
com.ibm.net.SocketKeepAliveParameters
773 EXAMPLES WARN [P=731088:O=0:CT] openjpa.Enhance - This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "
co.ne.staffmanagement.domain.Employee".
WSCL0100E: Exception received: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at com.ibm.ws.client.applicationclient.launchClient.createContainerAndLaunchApp(launchClient.java:790)
at com.ibm.ws.client.applicationclient.launchClient.main(launchClient.java:495)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at com.ibm.wsspi.bootstrap.WSLauncher.launchMain(WSLauncher.java:234)
at com.ibm.wsspi.bootstrap.WSLauncher.main(WSLauncher.java:95)
at com.ibm.wsspi.bootstrap.WSLauncher.run(WSLauncher.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.eclipse.core.launcher.Main.invokeFramework(Main.java:340)
at org.eclipse.core.launcher.Main.basicRun(Main.java:282)
at org.eclipse.core.launcher.Main.run(Main.java:981)
at com.ibm.wsspi.bootstrap.WSPreLauncher.launchEclipse(WSPreLauncher.java:376)
at com.ibm.wsspi.bootstrap.WSPreLauncher.main(WSPreLauncher.java:147)
Caused by: <openjpa-2.2.1-SNAPSHOT-r422266:1325904 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Attempt to cast instance "Employee Lucia OchoaManager10000 [co.ne.staffmanagement.domain.Employee]" to PersistenceCapable failed. Ensure that it has been enhanced.
FailedObject: Employee Lucia OchoaManager10000 [co.ne.staffmanagement.domain.Employee]
at org.apache.openjpa.kernel.BrokerImpl.assertPersistenceCapable(BrokerImpl.java:4665)
at org.apache.openjpa.kernel.BrokerImpl.persistInternal(BrokerImpl.java:2626)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2571)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2554)
at org.apache.openjpa.kernel.BrokerImpl.persist(BrokerImpl.java:2458)
at org.apache.openjpa.kernel.DelegatingBroker.persist(DelegatingBroker.java:1077)
at org.apache.openjpa.persistence.EntityManagerImpl.persist(EntityManagerImpl.java:716)
at co.ne.staffmanagement.test.TestHarness.main(TestHarness.java:21)
... 32 more
錯誤消息中提到了問題(org.apache.openjpa.persistence.ArgumentException:嘗試將實例「Employee Lucia OchoaManager10000 [co.ne.staffmanagement.domain.Employee]」強制轉換爲PersistenceCapable失敗。確保它具有被增強。)。你必須增強你的實體。查看更多關於增強功能的信息:http://struberg.wordpress.com/2012/01/08/jpa-enhancement-done-right/,http://openjpa.apache.org/builds/1.1.0/docs/ref_guide_pc_enhance .html#ref_guide_pc_enhance_runtime_container – aniri
非常感謝您的信息。最後,解決方案是,並在閱讀給出的鏈接後,修改persistence.xml,如下所示:只需添加以下屬性: –
請添加評論作爲答案並接受它? – Pool