如何编辑jdoconfig.xml和persistence.xml,以便JPA保存到appengine数据存储区

时间:2013-04-04 21:06:50

标签: google-app-engine jpa-2.0 google-eclipse-plugin

使用Google Eclipse插件,我的项目会自动在META-INF文件夹中附带两个文件:jdoconfig.xmlpersistence.xml。根据{{​​3}}上的说明,我的持久性文件应该包含以下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文件?现在,我对persistence进行了更改,但jdoconfig更改了,我得到了一个巨大的错误跟踪。

编辑错误:

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)

1 个答案:

答案 0 :(得分:6)

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

首先需要确定要使用的持久性机制,我会假设它是JPA,所以实际上你可以删除jdoconfig.xml

有了这样说,确保Datanucleus的所有必需库都在你的CLASSPATH中,最重要的是,persisence.xml必须在你的CLASSPATH的ROOT中。

我已经添加了成功的JPA 2 / Datanucleus persitence所需的lib图片。

还要确保正确配置了增强器。

仅供参考:我永远无法使用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的包/文件夹中查找要增强的类。

祝你好运

Babajide

enter image description here