如何编辑jdoconfig.xml和persistence.xml,以便将JPA保存到Appengine数据存储区 [英] How to edit jdoconfig.xml and persistence.xml so JPA saves to appengine datastore

查看:80
本文介绍了如何编辑jdoconfig.xml和persistence.xml,以便将JPA保存到Appengine数据存储区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the Google Eclipse Plugin, my project automatically comes with two files inside the META-INF folder: jdoconfig.xml and persistence.xml. Per the instructions on https://developers.google.com/appengine/docs/java/datastore/jpa/overview, my persistence file is supposed to have the following line for jpa datastore storage:

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

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

But when I open the persistence file I found

<?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文件编辑为这样

With those default contents, JPA has not been saving to my datastore. So I edit the persistence.xml file to look like this

<?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的更改却保持不变,

How should I change the jdoconfig.xml file? Right now, with my changes to persistence but jdoconfig as is, I am getting a huge error trace.

已删除的错误:

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)

推荐答案

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

For JPA 2.0 The provider is org.datanucleus.api.jpa.PersistenceProviderImpl Please note that persistence.xml is the configuration file used by JPA and jdoconfig.xml is used if you want to use JDO.

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

You first need to decide what persistence mechanism you want to use, I would assume its JPA so in fact, you can delete jdoconfig.xml.

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

With that been said, make sure all the reguired libs for Datanucleus is in your CLASSPATH and most importantly, persisence.xml must be in the ROOT of your CLASSPATH.

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

I have added a picture of the lib needed for a successful JPA 2 / Datanucleus persitence.

还请确保您的增强器已正确配置.

Also make sure your enhancer is correctly configured.

仅供参考:我永远无法使Google Eclipse插件与JPA 2配合使用,而实体增强功能却从未奏效,因此我使用了maven.有几种方法可以增强您的课程,而maven就是其中一种.

FYI: I could never get Google Eclipse Plugin to work with JPA 2, the entity enhancements never worked so I used maven. There are several ways to enhance your classes and maven is one.

这是我的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的插件"部分:

For the enhancements, add the following to plugins section of your 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>更改为放置实体的包.

Change <mappingIncludes>**/entity/*.class</mappingIncludes> to the package where your entities are placed.

在我自己的情况下,DataNucleus Enhancer将在名为entity的程序包/文件夹中寻找要增强的类.

In my own case, DataNucleus Enhancer will look for classes to enhance in package/folder named entity.

祝你好运

巴巴吉德

这篇关于如何编辑jdoconfig.xml和persistence.xml,以便将JPA保存到Appengine数据存储区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆