关于这一点必须有很多问题,我已经阅读了一些,但答案仍然没有得到答案。我是JPA的新手,我只是想测试一个简单的应用程序,看看我是否可以正确配置它。它是一个独立的应用程序,意味着它不会与Web服务器或任何东西一起运行。实体类看起来像:
@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;
}
}
主要:
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");
}
}
配置:
<?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>
控制台打印输出:
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)
我的问题
我猜主要的问题是,我做错了什么。我对此非常陌生,我正试图让这个独立的应用程序工作,以便我可以扩展它以供现实世界使用。
2.我是否遗漏了除persistence.xml文件以外的其他一些配置?
3.为独立应用程序解决此错误的最简单方法是什么?
非常感谢提前!
答案 0 :(得分:7)
我看到你有主类,因此我假设你在Java SE环境中使用它。使其工作的最简单方法是在命令行中定义-javaagent,如下所示:
java -jar myJAR.jar -javaagent:openjpa-all-2.3.0.jar
也可以从Eclipse:运行 - >运行配置 - >在“Java应用程序”中找到您的应用程序 - &gt;参数 - &gt; VM参数 - &gt;添加
-javaagent:/full/path/to/openjpa-all-2.3.0.jar
答案 1 :(得分:0)
通过检查[ManagedClassSubclasser]
1代码,您可以看到这更像是一个警告。如果要使此异常静音并通过以下方式记录警告:
<property name="openjpa.RuntimeUnenhancedClasses" value="warn"/>
答案 2 :(得分:0)
我想在面对同样的问题时添加我的经验。如果你使用tomcat它没有实现OpenJPA的完整规范,你需要单独提供增强功能。有关增强功能,请按照此链接http://openjpa.apache.org/enhancement-with-eclipse.html上提供的说明进行操作。你会很高兴。 注意:我知道您没有使用任何服务器,但是如果您想使用tomcat,那么您需要关注该链接。