openJpa

Apr 25, 2014

今天搞个JPA试试。选择了openjpa 的实现。遇到的问题多多啊。

  • 1. persistence.xml 中 class 要在 properties 之前,否则会报出

Exception in thread “main” javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for “sjpa” after trying the following discovered implementations: org.apache.openjpa.persistence.PersistenceProviderImpl from provider: org.apache.openjpa.persistence.PersistenceProviderImpl at javax.persistence.Persistence.createPersistenceException(Persistence.java:244) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:186) at net.microwww.sjpa.Main.main(Main.java:43) Caused by: <openjpa-2.2.2-r422266:1468616 nonfatal general error> org.apache.openjpa.util.GeneralException: org.xml.sax.SAXException: file:/E:/DevelopProject/sjpa/target/classes/META-INF/persistence.xml [Location: Line: 26, C: 12]: org.xml.sax.SAXParseException; systemId: file:/E:/DevelopProject/sjpa/target/classes/META-INF/persistence.xml; lineNumber: 26; columnNumber: 12; cvc-complex-type.2.4.d: 发现了以元素 ‘class’ 开头的无效内容。此处不应含有子元素。 at org.apache.openjpa.lib.meta.XMLMetaDataParser.parseNewResource(XMLMetaDataParser.java:427) at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:347) at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:324) at org.apache.openjpa.lib.meta.XMLMetaDataParser.parse(XMLMetaDataParser.java:297) at org.apache.openjpa.persistence.PersistenceProductDerivation$ConfigurationParser.parse(PersistenceProductDerivation.java:808) at org.apache.openjpa.persistence.PersistenceProductDerivation.parseResources(PersistenceProductDerivation.java:575) at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:527) at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:338) at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:86) at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:153) at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:62) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:152) … 1 more

  • 2. openjpa-2.2.2 在 JDK1.7 会报出下面的错误,需要关闭 jpa自带的增强功能 <property name="openjpa.DynamicEnhancementAgent" value="false" />(persistence.xml中),这时会报出第三个错误(见下面)

java.lang.LinkageError: loader (instance of  sun/misc/Launcher$AppClassLoader): attempted  duplicate class definition for name: “org/apache/openjpa/jdbc/identifier/DBIdentifier$DBIdentifierType” at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423)

  • 3. 关闭了自动增强,需要编译的时候增强 ,否则会报出下面的异常,apache 提供了三种增强方式:移步 官网 。

Exception in thread “main” <openjpa-2.2.2-r422266:1468616 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: “ net.microwww.sjpa.domain.Account”. at org.apache.openjpa.enhance.ManagedClassSubclasser.prepareUnenhancedClasses(ManagedClassSubclasser.java:115) at org.apache.openjpa.kernel.AbstractBrokerFactory.loadPersistentTypes(AbstractBrokerFactory.java:314) at org.apache.openjpa.kernel.AbstractBrokerFactory.initializeBroker(AbstractBrokerFactory.java:238) at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:212) at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:156) at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:227) at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:154) at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:60) at net.microwww.sjpa.Main.main(Main.java:49)