gpt4 book ai didi

google-app-engine - 如何编辑 jdoconfig.xml 和 persistence.xml 以便 JPA 保存到 appengine 数据存储

转载 作者:行者123 更新时间:2023-12-01 13:55:25 25 4
gpt4 key购买 nike

使用 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文件?现在,随着我对 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)

最佳答案

对于 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 的包/文件夹中查找要增强的类。 .

祝你好运

巴巴吉德

enter image description here

关于google-app-engine - 如何编辑 jdoconfig.xml 和 persistence.xml 以便 JPA 保存到 appengine 数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com