JPA不会从CloudSQL返回任何数据

时间:2016-01-06 16:53:13

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

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

<%= link_to retailer_my_stations_path, class: 'dropdown-collapse' do %>
  <i class='icon-edit'></i>
  <span>My Stations</span>
<% end %>

我的persistence.xml

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

pom.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>

和JPA的servlet

<?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>

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

1 个答案:

答案 0 :(得分:-1)

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