2

使用 Google Eclipse 插件,我的项目会自动在 META-INF 文件夹中包含两个文件:jdoconfig.xmlpersistence.xml. 根据https://developers.google.com/appengine/docs/java/datastore/jpa/overview上的说明,我的持久性文件应该具有以下用于 jpa 数据存储存储的行:

<provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>

但是当我打开持久性文件时,我发现

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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_1_0.xsd" version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
        </properties>
    </persistence-unit>
</persistence>

jdoconfig.xml文件是

<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

   <persistence-manager-factory name="transactions-optional">
       <property name="javax.jdo.PersistenceManagerFactoryClass"
           value="org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManagerFactory"/>
       <property name="javax.jdo.option.ConnectionURL" value="appengine"/>
       <property name="javax.jdo.option.NontransactionalRead" value="true"/>
       <property name="javax.jdo.option.NontransactionalWrite" value="true"/>
       <property name="javax.jdo.option.RetainValues" value="true"/>
       <property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
   </persistence-manager-factory>
</jdoconfig>

使用这些默认内容,JPA 并没有保存到我的数据存储中。所以我编辑persistence.xml文件看起来像这样

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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_1_0.xsd" version="1.0">

    <persistence-unit name="transactions-optional">
        <provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
        </properties>
    </persistence-unit>
</persistence>

我应该如何更改jdoconfig.xml文件?现在,随着我的更改,persistencejdoconfig原样,我得到了一个巨大的错误跟踪。

编辑错误:

java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
    at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)

…
…
...

Caused by: javax.persistence.PersistenceException: No persistence providers available for "transactions-optional" after trying the following discovered implementations: org.datanucleus.api.jpa.PersistenceProviderImpl
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:180)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:70)
4

1 回答 1

6

对于 JPA 2.0,提供程序是org.datanucleus.api.jpa.PersistenceProviderImpl请注意,这persistence.xml是 JPA 使用的配置文件,jdoconfig.xml如果您想使用 JDO,则使用它。

您首先需要决定要使用哪种持久性机制,我假设它的 JPA 所以实际上,您可以删除jdoconfig.xml.

话虽如此,请确保 Datanucleus 所需的所有库都在您的 CLASSPATH 中,最重要的是,persisence.xml 必须在您的 CLASSPATH 的根目录中。

我添加了成功 JPA 2 / Datanucleus 持久性所需的库的图片。

还要确保您的增强器配置正确。

仅供参考:我永远无法让 Google Eclipse 插件与 JPA 2 一起使用,实体增强从未起作用,所以我使用了 maven。有几种方法可以增强你的类,maven 就是其中之一。

这是我的pom。

    <properties>
            <!-- Convenience property to set the GWT version -->
            <gwtVersion>2.5.0</gwtVersion>
            <gxtVersion>2.2.5</gxtVersion>
            <gae.version>1.7.5</gae.version>
            <datanucleus.version>3.1.3</datanucleus.version>
            <!-- GWT needs at least java 1.5 -->
            <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>

    <!-- DN -->
        <dependency>
            <groupId>com.google.appengine.orm</groupId>
            <artifactId>datanucleus-appengine</artifactId>
            <version>2.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-core</artifactId>
            <version>${datanucleus.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-api-jpa</artifactId>
            <version>${datanucleus.version}</version>
        </dependency>

        <dependency>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-api-jdo</artifactId>
            <version>${datanucleus.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.jdo</groupId>
            <artifactId>jdo-api</artifactId>
            <version>3.0.1  </version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jpa_2.0_spec</artifactId>
            <version>1.0</version>
        </dependency>

    <dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-jsr107cache</artifactId>
  <version>${gae.version}</version>
</dependency>

    <dependency>
  <groupId>com.google.appengine</groupId>
  <artifactId>appengine-endpoints</artifactId>
  <version>${gae.version}</version>
</dependency>

    <dependency>
  <groupId>net.sf.jsr107cache</groupId>
  <artifactId>jsr107cache</artifactId>
  <version>1.1</version>
</dependency>

对于增强功能,请将以下内容添加到 pom 的插件部分:

<plugin>
                <groupId>org.datanucleus</groupId>
                <artifactId>maven-datanucleus-plugin</artifactId>
                <version>${datanucleus.version}</version>
                <configuration>
                    <api>JPA</api>
                    <verbose>true</verbose>             
                     <mappingIncludes>**/entity/*.class</mappingIncludes>
                     <fork>false</fork>
                     <log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
                </configuration>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

更改<mappingIncludes>**/entity/*.class</mappingIncludes>为放置实体的包。

在我自己的情况下,DataNucleus Enhancer 将在名为entity.

祝你好运

巴巴吉德

在此处输入图像描述

于 2013-04-05T06:53:49.027 回答