Enhance class for JPA Entity
Hiya
I am just getting started with JPA and running a small test to see if I can get everything configured correctly (and I can't).
The error I am getting when I try to submit a entity to the database is:
<openjpa-2.4.0-r422266:1674604 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: "
com.fh.SamEntity".
I am trying to build this in Intellij Idea. My persistence.xml is located at WEB-INF\classes\META-INF\persistence.xml and looks like:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="samDB">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<class>com.fh.SamEntity</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/sys"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionUserName" value="root"/>
<property name="openjpa.ConnectionPassword" value="agg530"/>
<property name="openjpa.Log" value="DefaultLevel=TRACE, Tool=INFO"/>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
For the most part it was auto generated by Idea - but I had to move it to \WEB-INF\classes as originally it was under \Main\Groovy\META-INF
My entity class looks like:
package com.fh;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import javax.persistence.*;
import java.sql.Date;
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "sam", schema = "sys", catalog = "")
public class SamEntity {
private int idSam;
private Date startDate;
private String address;
private Date endDate;
private Date attendDate;
....
....
....
@Basic
@Column(name = "StartDate")
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
@Basic
@Column(name = "Address")
public String getAddress() {
return address;
}
...
...
And the actual code to write the object to the Db:
package com.fh;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class harness {
static void testHarness(SamEntity sam1) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("samDB");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(sam1);
tx.commit();
em.close();
}
Note that this project is a servlet. I think I need to make the class enhanceable or add -javaagent:"C:\path\to\openjpa-2.4.0.jar" but when I did this I got:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:401)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
at org.apache.openjpa.enhance.PCEnhancerAgent.premain(PCEnhancerAgent.java:124)
... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Exception in thread "main" Disconnected from server
Also my project is being built with gradle.
Any assistance appreceated and thank you in advance,
-Al
请先登录再写评论。
The first warning doesn't seem to be relevant, the second one is caused by a missed dependency, looks like you don't have commons-lang in the classpath: http://stackoverflow.com/a/16152733/104891.
commons-lang-2.4 is in WEB-INF\lib
It compiles if I dont include the javaagent agent option, but I read that I need the java agent option to avoid this error on the debug console:
<openjpa-2.4.0-r422266:1674604 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: "
com.fh.SamEntity".
Which is generated on the EntityManager and EntityTransaction lines
On the actual console itself I get:
846 samDB WARN [http-nio-8080-exec-5] openjpa.jdbc.Schema - Existing column "Unborn" on table "sys.sam" is incompatible with the same column in the given schema definition. Existing column:
Full Name: sam.Born
Type: varchar
Size: 45
Default: null
Not Null: false
Given column:
Full Name: sam.Born
Type: varchar
Size: 255
Default: null
Not Null: false
852 samDB WARN [http-nio-8080-exec-5] openjpa.jdbc.Schema - Existing column "Weapon" on table "sys.sam" is incompatible with the same column in the given schema definition. Existing column:
Full Name: sam.WeaponType
Type: varchar
Size: 50
Default: null
Not Null: false
Given column:
Full Name: sam.WeaponType
Type: varchar
Size: 255
Default: null
Not Null: false
881 samDB TRACE [http-nio-8080-exec-5] openjpa.Runtime - Found datasource1: datasource 1321016407 from configuration. StoreContext: org.apache.openjpa.kernel.FinalizingBrokerImpl@1f65ebe3
C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\catalina.bat stop
Using CATALINA_BASE: "C:\Users\AlGrant\.IntelliJIdea2016.3\system\tomcat\Unnamed_cmfhapp"
Using CATALINA_HOME: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4"
Using CATALINA_TMPDIR: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_101"
Using CLASSPATH: "C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\bootstrap.jar;C:\Users\AlGrant\Documents\apache-tomcat-8.5.4\bin\tomcat-juli.jar"
14-Mar-2017 16:43:43.532 INFO [main] org.apache.catalina.core.StandardServer.await A valid shutdown command was received via the shutdown port. Stopping the Server instance.
14-Mar-2017 16:43:43.533 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["http-nio-8080"]
14-Mar-2017 16:43:43.584 INFO [main] org.apache.coyote.AbstractProtocol.pause Pausing ProtocolHandler ["ajp-nio-8009"]
14-Mar-2017 16:43:43.635 INFO [main] org.apache.catalina.core.StandardService.stopInternal Stopping service Catalina
14-Mar-2017 16:43:43.648 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [cmfhapp-1.0-SNAPSHOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
14-Mar-2017 16:43:43.649 WARNING [localhost-startStop-2] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [cmfhapp-1.0-SNAPSHOT] appears to have started a thread named [Abandoned connection cleanup thread] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
com.mysql.jdbc.AbandonedConnectionCleanupThread.run(AbandonedConnectionCleanupThread.java:43)
14-Mar-2017 16:43:43.659 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["http-nio-8080"]
14-Mar-2017 16:43:43.661 INFO [main] org.apache.coyote.AbstractProtocol.stop Stopping ProtocolHandler ["ajp-nio-8009"]
14-Mar-2017 16:43:43.661 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["http-nio-8080"]
14-Mar-2017 16:43:43.662 INFO [main] org.apache.coyote.AbstractProtocol.destroy Destroying ProtocolHandler ["ajp-nio-8009"]
Disconnected from server
Does this help? Thanks Serge.
So, what's the actual error? Does it prevent your app from working?
Well yes. Nothing ends up in the database....and if I put a println statement here :
The "Here" statement is never hit - despite all the output from the attempt to write to the Db.
Please share the project to reproduce the problem: https://intellij-support.jetbrains.com/hc/articles/206869619.