9

我创建了小型 JPA 项目来保存学生记录。我使用 Oracle 数据库。我使用 OpenJPA 作为 JPa 提供程序。

我已经正确创建了 Table student 和相关序列。

学生实体类

@Entity
@Table(name = "Student")
public class Student implements Serializable {

    private int id;
    private String name;
    private static final long serialVersionUID = 1L;

    public Student() {
        super();
    }

    @Id
    @Column(name = "ID")
    @SequenceGenerator(name = "TRAIN_SEQ", sequenceName = "STUDENT_SEQ")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TRAIN_SEQ")
    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "NAME")
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

持久性.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="JPAOracleDemo">

        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>

        <class>com.jpa.demo.model.Student</class>

        <properties>
             <property name="openjpa.ConnectionURL" value="jdbc:oracle:thin:@TEST:50111:TESTPEGAD1" />
            <property name="openjpa.ConnectionDriverName" value="oracle.jdbc.driver.OracleDriver" />
            <property name="openjpa.ConnectionUserName" value="admin" />
            <property name="openjpa.ConnectionPassword" value="admin" />
            <property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
            <property name="openjpa.jdbc.Schema" value="MYSCHEMA" />
        </properties>

    </persistence-unit>

</persistence>

客户端类

OpenJPAEntityManager em = JPAUtil.getEntityManager();
        OpenJPAEntityTransaction tx = em.getTransaction();
        tx.begin();

        // Create the instance of Employee Entity class
        Student student = new Student();
        student.setName("A.Ramesh");

        // JPA API to store the Student instance on the database.
        em.persist(student);
        tx.commit();
        em.close();

        System.out.println("Done...");

实用类

private static OpenJPAEntityManagerFactory emf = OpenJPAPersistence
            .createEntityManagerFactory("JPAOracleDemo", "META-INF/persistence.xml");

    private static OpenJPAEntityManager entManager;

    /**
     * No need to create any instance for this Util.
     */
    private JPAUtil() {
    }

    /**
     * Get {@link EntityManager}.
     * 
     * @return the {@link EntityManager}
     */
    public static OpenJPAEntityManager getEntityManager() {

        if (entManager == null || !entManager.isOpen()) {
            entManager = emf.createEntityManager();
        }

        return entManager;
    }

数据成功保存在学生表中,但出现以下错误

Exception in thread "Attachment 60230" java.lang.UnsupportedOperationException: cannot get the capability, performing dispose of the retransforming environment
    at com.ibm.tools.attach.javaSE.Attachment.loadAgentLibraryImpl(Native Method)
    at com.ibm.tools.attach.javaSE.Attachment.loadAgentLibrary(Attachment.java:253)
    at com.ibm.tools.attach.javaSE.Attachment.parseLoadAgent(Attachment.java:235)
    at com.ibm.tools.attach.javaSE.Attachment.doCommand(Attachment.java:154)
    at com.ibm.tools.attach.javaSE.Attachment.run(Attachment.java:116)
Exception in thread "main" java.lang.UnsupportedOperationException: cannot get the capability, performing dispose of the retransforming environment
    at sun.instrument.InstrumentationImpl.isRetransformClassesSupported0(Native Method)
    at sun.instrument.InstrumentationImpl.isRetransformClassesSupported(InstrumentationImpl.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:600)
    at org.apache.openjpa.enhance.ClassRedefiner.canRedefineClasses(ClassRedefiner.java:123)
    at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:122)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:304)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:228)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:202)
    at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:213)
    at com.ibm.ws.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:45)
    at com.ibm.ws.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:30)
    at com.jpa.demo.util.JPAUtil.getEntityManager(JPAUtil.java:32)
    at com.jpa.demo.client.JPAClient.main(JPAClient.java:13)
1045  JPAOracleDemo  INFO   [main] openjpa.Enhance - Creating subclass for "[class com.jpa.demo.model.Student]". This means that your application will be less efficient and will consume more memory than it would if you ran the OpenJPA enhancer. Additionally, lazy loading will not be available for one-to-one and many-to-one persistent attributes in types using field access; they will be loaded eagerly instead.
Done...

爪哇版

JDK 1.6

有人请让我知道这里有什么问题吗?

更新:

我使用 IBM Rational Software Architect for Websphere Software 进行此开发。这个问题出在这个 IDE 上。当我默认创建 JPA 项目时,它会添加 IBM jre。我刚刚删除了 IBM jre 并尝试使用 SUN jre 然后它成功了。请让我知道为什么这个功能不支持 IBM jre?

4

3 回答 3

2

<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />

首先,摆脱该属性。

于 2013-06-11T20:54:29.283 回答
1

这是我的增强器模板,它适用于 OPENJPA:`

<path id="enhance.cp">
       <pathelement location="${basedir}${file.separator}${build.dir}" />
        <fileset dir="${basedir}${file.separator}ext_libs/">
          <include name="**/*.jar" />
    </fileset>
</path>
<property name="cp" refid="enhance.cp" />
 <target name="openjpa.libs.check" unless="openjpa.libs">
    <fail message="Please set -Dopenjpa.libs in your builder configuration!" />
 </target>
 <target name="build.dir.check" unless="build.dir">
    <fail message="Please set -Dbuild.dir in your builder configuration!" />
</target>
<target name="enhance" depends="openjpa.libs.check, build.dir.check">
    <echo message="${cp}" />
    <taskdef name="openjpac" classname="org.apache.openjpa.ant.PCEnhancerTask">
        <classpath refid="enhance.cp" />
    </taskdef>
    <openjpac>
        <classpath refid="enhance.cp" />
     <configpropertiesFile="${basedir}${file.separator}src${file.separator}main${file.separator}               resources${file.separator}META-INF${file.separator}persistence.xml" />
    </openjpac>
</target>

`

于 2013-08-09T07:00:08.993 回答
1

JPA 规范要求对 Entity 对象进行某种类型的监控,但该规范没有定义如何实现这种监控。一些 JPA 提供程序会在运行时自动生成新的子类或代理对象,这些子类或代理对象位于用户的 Entity 对象前面,而另一些则使用字节码编织技术来增强实际的 Entity 类对象。OpenJPA 支持这两种机制,但强烈建议仅使用字节码编织增强。不推荐子类化支持(由 OpenJPA 提供)(在 OpenJPA 2.0 及更高版本中默认禁用)。(来源: http: //openjpa.apache.org/entity-enhancement.html

这个问题的原因是我使用了实体增强的子类化支持,但在 OpenJPA2.0 及更高版本中默认禁用。

我找到了这个问题的解决方案。我们必须通过在启动 OpenJPA 将在其中运行的 JVM 时提供 javaagent 来增强运行时的实体类。

我将以下内容作为 JVM 参数

-javaagent:C:/OpenJPA/apache-openjpa-2.0.0/openjpa-2.0.0.jar

我从persistence.xml中删除了下面的行

<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />

工作持久性.xml

<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="DataSourceDemo">

        <jta-data-source>oracleDS</jta-data-source>
        <class>com.auditlog.model.BatchPrint</class>

        <properties>
            <property name="openjpa.ConnectionUserName" value="admin" />
            <property name="openjpa.ConnectionPassword" value="test" />
            <property name="openjpa.jdbc.Schema" value="defaultScheme" />
        </properties>

    </persistence-unit>
</persistence>
于 2013-09-02T09:01:42.050 回答