Exception regarding runtime optimization using openJPA MySQL

这一生的挚爱 提交于 2019-12-06 06:11:08

问题


There must be a bunch of questions regarding this, and I have read a few, but the answer still eludes me. I am new to JPA and I am just trying to test a simple application to see if I can configure the thing properly. It is a stand alone application meaning it will not be run with a web server or anything. The entity class looks like:

@Entity
public class Person{

    @Id
    private String userID = null;

    @Transient
    private UserState userState = null;

    private String email = null;
    private String name = null;


    public Person(){
        userID = null;
        email = null;
        name = null;
        userState = null;
    }

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getName() {
        return name;
    }

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

    public UserState getUserState() {
        return userState;
    }

    public void setUserState(UserState userState) {
        this.userState = userState;
    }

}

The main:

public class PersistenceTest {

    public static void main(String[] args) {
        System.out.println("creating person");
        Person p = new Person();
        p.setUserID("GregR");
        p.setEmail("Gregory@company.de");
        p.setUserState(UserState.ACTIVE);
        System.out.println("done creating person GregR");


        EntityManagerFactory factory = Persistence.createEntityManagerFactory(PersonService.PERSISTENCE_UNIT_NAME);
        System.out.println("factory initialized");
        EntityManager manager = factory.createEntityManager();
        System.out.println("EntityManager initialized");
        PersonService service = new PersonService(manager);
        System.out.println("service initialized");

        System.out.println("Beginning transaction");
        manager.getTransaction().begin();
        System.out.println("Transaction begun");

        System.out.println("attempting to persist person");
        service.persistEntity(p);
        manager.getTransaction().commit();
        System.out.println("person persisted");

        System.out.println("beginning cleanup");
        manager.close();
        factory.close();
        System.out.println("Cleanup has completed");
    }
}

The config:

<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="PersonService" transaction-type="RESOURCE_LOCAL">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
        <class>de.hol.persistable.entities.Person</class>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/ConnectionWikisDB"/>
            <property name="javax.persistence.jdbc.user" value="GregR"/>
            <property name="javax.persistence.jdbc.password" value="myPassword"/>
        </properties>
    </persistence-unit>
</persistence>

The console printout:

creating person
done creating person GReeder
factory initialized
47  PersonService  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.3.0
110  PersonService  INFO   [main] openjpa.jdbc.JDBC - Using dictionary class "org.apache.openjpa.jdbc.sql.MySQLDictionary".
306  PersonService  INFO   [main] openjpa.jdbc.JDBC - Connected to MySQL version 5.5 using JDBC driver MySQL-AB JDBC Driver version mysql-connector-java-5.0.8 ( Revision: ${svn.Revision} ). 
Exception in thread "main" <openjpa-2.3.0-r422266:1540826 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: This configuration disallows runtime optimization, but the following listed types were not enhanced at build time or at class load time with a javaagent: "
de.hol.persistable.entities.Person".
    at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:115)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:312)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:236)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:212)
    at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:155)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:59)
    at de.hol.persistable.PersistenceTest.main(PersistenceTest.java:24)

My Questions
1. I guess the main question is, what am I doing wrong. I am very new to this and am trying to just get this stand alone application to work so that I can expand it for real world use.
2. Am I missing some other configuration other than the persistence.xml file?
3. What is the simplest way of getting around this error for a stand-alone app?

Many thanks in advance!


回答1:


I see you have main class, therefore I assume you are using it in Java SE environment. The easiest way to make it work is to define -javaagent in command line, like this:

java -jar myJAR.jar -javaagent:openjpa-all-2.3.0.jar

It is also possible from Eclipse: Run->Run Configurations->find your application in "Java Applications"->Arguments->VM arguments->add

-javaagent:/full/path/to/openjpa-all-2.3.0.jar



回答2:


By inspecting [ManagedClassSubclasser]1 code you can see that this is more like a warning. If you want to silence this exception and log a warning instead by:

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



回答3:


I would like to add my experience while faced same kind of problem. If you are usng the tomcat it doesn't implement the full spec for OpenJPA you need to provide the enhancements separately. For the enhancement follow the instructions available on this link http://openjpa.apache.org/enhancement-with-eclipse.html. And you will be good to go. NOTE: i know you are not using any server, but in case you want to use tomcat then you need to follow the link.



来源:https://stackoverflow.com/questions/24994216/exception-regarding-runtime-optimization-using-openjpa-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!