2

我是 JPA 的新手。

我创建了一个 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();

    }

}

实体:

package co.ne.staffmanagement.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class Employee implements java.io.Serializable {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;
    private String firstName;
    private String surname;
    private String jobRole;
    private int salary;

    public Employee(){
        // Required by the JPA
    }

    public Employee(String firstName, String surname, String jobRole, int salary){
        super();
        this.firstName = firstName;
        this.surname = surname;
        this.jobRole = jobRole;
        this.salary = salary;
    }

    public String toString(){
        return "Employee " + this.firstName + " " + this.surname + this.jobRole + this.salary;
    }
}

持久性.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
4

2 回答 2

1

这对我有用:

我添加了 <property name="openjpa.DynamicEnhancementAgent" value="true"/>

到我的 persistence.xml 文件

于 2015-06-18T20:17:56.713 回答
0

实际上不建议使用动态增强器(不要将其与 EE 环境 Class Transformer 或 java 代理混淆),因为它存在许多问题。对于 SE(瘦客户端、应用程序客户端)环境,我建议预先增强您的实体类(2.1。在构建时增强:http: //openjpa.apache.org/builds/2.4.0/apache-openjpa/ docs/ref_guide_pc_enhance.html#ref_guide_pc_enhance_build)。

于 2015-10-22T17:37:37.747 回答