【问题标题】:JPA does not return any data from CloudSQLJPA 不从 CloudSQL 返回任何数据
【发布时间】:2016-04-10 20:31:49
【问题描述】:

我有基于 maven 的项目,用于在 GoogleAppEngine 上进行部署。我有本地 MySQL 数据库(用于开发)和远程 CloudSQL 数据库(用于生产)
我的代码基于google github
CloudSQL 和 MySQL 中的数据是相同的。尝试通过生产中的命名查询检索数据时遇到问题 - 返回大小为 0 的列表。在本地 MySQL DB 代码上正确返回数据。我什至尝试使用标准 java.sql.Connection 从 ClodSQL 检索数据,并且能够检索数据。仅当从 JPA 请求时才返回数据。 使用的命名查询:

@NamedQueries({(name = "Device.getDevices", = "SELECT d FROM Device d")

我的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-device">
    <class>commander.apprecorder.database.pojos.device.Device</class>

    <properties>
        <property name="javax.persistence.jdbc.user" value="root"/>
        <property name="datanucleus.autoCreateSchema" value="true"/>
    </properties>
</persistence-unit>
</persistence>

pom.xml 的内容

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>

<groupId>group</groupId>
<artifactId>art</artifactId>

<properties>
    <app.id>appid</app.id>
    <app.version>version</app.version>
    <appengine.version>1.9.30</appengine.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gcloud.plugin.version>2.0.9.74.v20150814</gcloud.plugin.version>

    <objectify.version>5.1.5</objectify.version>
    <guava.version>18.0</guava.version>

    <datanucleus.jpa.version>3.1.1</datanucleus.jpa.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.showDeprecation>true</maven.compiler.showDeprecation>
    <cloudsql.device.url>
        jdbc:google:mysql://some:instance:name/db?user=xxx
    </cloudsql.device.url>
    <cloudsql.device.url.dev>jdbc:mysql://localhost/db?user=x</cloudsql.device.url.dev>

    <datanucleus-core.version>4.0.0-release</datanucleus-core.version>
    <datanucleus-maven-plugin.version>4.0.0-release</datanucleus-maven-plugin.version>
    <datanucleus-accessplatform-jpa-rdbms.version>4.0.0-release</datanucleus-accessplatform-jpa-rdbms.version>
    <javax.persistence.version>2.1.0</javax.persistence.version>
</properties>

<prerequisites>
    <maven>3.1.0</maven>
</prerequisites>

<dependencies>
    <!-- Compile/runtime dependencies -->
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>${appengine.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.appengine.tools</groupId>
        <artifactId>appengine-gcs-client</artifactId>
        <version>0.5</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20151123</version>
    </dependency>


    <!--JPA start -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.25</version>
    </dependency>
    <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>datanucleus-accessplatform-jpa-rdbms</artifactId>
        <version>${datanucleus-accessplatform-jpa-rdbms.version}</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>org.datanucleus</groupId>
        <artifactId>javax.persistence</artifactId>
        <version>${javax.persistence.version}</version>
    </dependency>
    <!--JPA end -->

    <!-- [START Objectify_Dependencies] -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.objectify</groupId>
        <artifactId>objectify</artifactId>
        <version>${objectify.version}</version>
    </dependency>
    <!-- [END Objectify_Dependencies] -->

    <!-- Test Dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>2.0.2-beta</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-testing</artifactId>
        <version>${appengine.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-stubs</artifactId>
        <version>${appengine.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>

    <!-- for hot reload of the web application-->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.1</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>display-dependency-updates</goal>
                        <goal>display-plugin-updates</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <version>3.2</version>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.datanucleus</groupId>
                    <artifactId>datanucleus-core</artifactId>
                    <version>${datanucleus-core.version}</version>
                    <scope>compile</scope>
                </dependency>
            </dependencies>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archiveClasses>true</archiveClasses>
                <webResources>
                    <!-- in order to interpolate version from pom into appengine-web.xml -->
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>${appengine.version}</version>
            <configuration>
                <enableJarClasses>false</enableJarClasses>
                <version>${app.version}</version>
                <!-- Comment in the below snippet to bind to all IPs instead of just localhost -->
                <!-- address>0.0.0.0</address>
                <port>8080</port -->
                <!-- Comment in the below snippet to enable local debugging with a remote debugger
                     like those included with Eclipse or IntelliJ -->
                <!-- jvmFlags>
                  <jvmFlag>-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=n</jvmFlag>
                </jvmFlags -->
            </configuration>
        </plugin>
        <plugin>
            <groupId>com.google.appengine</groupId>
            <artifactId>gcloud-maven-plugin</artifactId>
            <version>${gcloud.plugin.version}</version>
            <configuration>
                <set_default>true</set_default>
            </configuration>
        </plugin>
        <!--DB start-->
        <plugin>
            <groupId>org.datanucleus</groupId>
            <artifactId>datanucleus-maven-plugin</artifactId>
            <version>${datanucleus-maven-plugin.version}</version>
            <configuration>
                <api>JPA</api>
                <persistenceUnitName>transactions-optional-device</persistenceUnitName>
                <fork>false</fork>
                <!--<persistenceUnitName>Demo</persistenceUnitName>-->
                <!--<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>-->
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.datanucleus</groupId>
                    <artifactId>datanucleus-core</artifactId>
                    <version>${datanucleus-core.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <!--JPA end-->
    </plugins>
</build>

JPA 的 servlet

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/plain");

    Map<String, String> properties = new HashMap();
    if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
        properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.GoogleDriver");
        properties.put("javax.persistence.jdbc.url", System.getProperty("cloudsql.device.url"));
    } else {
        properties.put("javax.persistence.jdbc.driver", "com.mysql.jdbc.Driver");
        properties.put("javax.persistence.jdbc.url", System.getProperty("cloudsql.device.url.dev"));
    }

    // List all the rows.

        EntityManagerFactory emf = Persistence.createEntityManagerFactory(
                "transactions-optional-device", properties);
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        resp.getWriter().println("performing query");
        List<Device> result = em.createNamedQuery("Device.getDevices", Device.class).getResultList();
        resp.getWriter().println("Starting to list devices - " + result.size());
        for (Device g : result) {
            resp.getWriter().println(g.getA() + "_" + g.getB());
        }

        em.getTransaction().commit();
        em.close();

}

应用引擎日志中没有错误。你能告诉我有什么问题吗?谢谢:)

【问题讨论】:

  • 在您的 JPA 提供者的日志中?调用什么 SQL? LOG 还说了什么?
  • 谢谢,使用 JPA 日志我能够找出问题
  • 您那里的 jar 版本完全不一致...... datanucleus-api-jpa v3.1.1 与 datanucleus-core v4.0.0!为什么你在那里使用旧版本,我不知道。我看到的最新版本是4.1.7的datanucleus-accessplatform-jpa-rdbms
  • 我已经使用了您提到的 Google 项目,我需要做的就是更改 datanucleus-accessplatform-jpa-rdbms 依赖项以使用最新版本......在我的情况下,我使用的是 4.1.5 但是没有理由认为 4.1.7 会有所不同。根本不需要任何“datanucleus-core”、“datanucleus-api-jpa”依赖项,所以不知道为什么会有这些。

标签: maven google-app-engine jpa google-cloud-sql datanucleus


【解决方案1】:

我将日志级别设置为 ALL 并发现 Datanucleus 正在尝试访问数据存储。它在数据存储中创建了新表并检索了其内容 - 没有数据。
我不知道为什么它试图访问数据存储并且没有使用 CloudSQL,但没关系...
我用google github 中的教程替换了 Hibernate 的 DataNucles,它第一次运行
迁移很简单,只需要修改persistance.xml和pom.xml。

【讨论】:

  • 这并没有回答“问题”,或者试图理解你为什么会遇到问题,只是绕过它。此外,它会尝试访问数据存储,因为您的配置告诉它这样做......您在 persistence.xml 中定义了一个持久性属性来执行此操作,所以如果您不希望它创建一个表,那么为什么要问它到?
猜你喜欢
  • 2020-09-22
  • 1970-01-01
  • 2017-11-23
  • 2021-12-13
  • 2016-12-06
  • 2011-04-04
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多