Google app engine 将google app engine与JPA和云sql一起使用,不使用端点返回任何记录

Google app engine 将google app engine与JPA和云sql一起使用,不使用端点返回任何记录,google-app-engine,jpa,google-cloud-sql,Google App Engine,Jpa,Google Cloud Sql,我有一个Android项目和一个应用引擎连接项目。我正在使用以下工具: JPA v2, App Engine 1.7.6, Java 1.7编译器, Eclipse 4.2朱诺, 日食2.4.x 我正在使用云sql数据库。我能够在JPA和DB Persective窗口中成功连接,并返回ok查询数据。我已将我的应用程序引擎设置为将SQL开发连接到我的云SQL数据库 我有一个表定义如下: CREATE Table Test(codeid varchar(3) NOT NULL,codedesc va

我有一个Android项目和一个应用引擎连接项目。我正在使用以下工具: JPA v2, App Engine 1.7.6, Java 1.7编译器, Eclipse 4.2朱诺, 日食2.4.x

我正在使用云sql数据库。我能够在JPA和DB Persective窗口中成功连接,并返回ok查询数据。我已将我的应用程序引擎设置为将SQL开发连接到我的云SQL数据库

我有一个表定义如下:

CREATE Table Test(codeid varchar(3) NOT NULL,codedesc varchar(20) NOT NULL,PRIMARY KEY (codeid));
 import java.io.Serializable;
    import javax.persistence.*;



    @Entity
    public class Test implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String codeid;

    private String codedesc;

    public Test() {
    }

    public String getCodeid() {
    return this.codeid;
    }

    public void setCodeid(String codeid) {
this.codeid = codeid;
    }

    public String getCodedesc() {
        return this.codedesc;
    }

    public void setCodedesc(String codedesc) {
        this.codedesc = codedesc;
    }
    }
 @Api(name = "testendpoint" , version = "v1")
    public class TestEndpoint {

    /**
     * This method lists all the entities inserted in datastore.
     * It uses HTTP GET method and paging support.
     *
     * @return A CollectionResponse class containing the list of all entities
     * persisted and a cursor to the next page.
     */
    @ApiMethod(    httpMethod = "GET",    name = "listtest.list",     path = "ch/list")
    @SuppressWarnings({ "unchecked", "unused" })
    public CollectionResponse<Test> listTest(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

        EntityManager mgr = null;
        Cursor cursor = null;
        List<Test> execute = null;

        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select x from Test x");
            if (cursorString != null && cursorString != "") {
                cursor = Cursor.fromWebSafeString(cursorString);
                query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
            }

            if (limit != null) {
                query.setFirstResult(0);
                query.setMaxResults(limit);
            }

            execute = (List<Test>) query.getResultList();
            cursor = JPACursorHelper.getCursor(execute);
            if (cursor != null)
                cursorString = cursor.toWebSafeString();

            // Tight loop for fetching all entities from datastore and accomodate
            // for lazy fetch.
            for (Test obj : execute);
        } 
        finally 
        {
            mgr.close();
        }

            return CollectionResponse.<Test> builder().setItems(execute).setNextPageToken       (cursorString).build();
    }

    private boolean containsCodeheader(Test test) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            Test item = mgr
                    .find(Test.class, test.getCodeid());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

        private static EntityManager getEntityManager() {
        return EMF.get().createEntityManager();
    }

    }
<?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_2_0.xsd" version="2.0">

    <persistence-unit name="transactions-optional" transaction-type="RESOURCE_LOCAL">
        <provider></provider>
        <class>com.testApp.Test</class>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
实体类如下所示:

CREATE Table Test(codeid varchar(3) NOT NULL,codedesc varchar(20) NOT NULL,PRIMARY KEY (codeid));
 import java.io.Serializable;
    import javax.persistence.*;



    @Entity
    public class Test implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String codeid;

    private String codedesc;

    public Test() {
    }

    public String getCodeid() {
    return this.codeid;
    }

    public void setCodeid(String codeid) {
this.codeid = codeid;
    }

    public String getCodedesc() {
        return this.codedesc;
    }

    public void setCodedesc(String codedesc) {
        this.codedesc = codedesc;
    }
    }
 @Api(name = "testendpoint" , version = "v1")
    public class TestEndpoint {

    /**
     * This method lists all the entities inserted in datastore.
     * It uses HTTP GET method and paging support.
     *
     * @return A CollectionResponse class containing the list of all entities
     * persisted and a cursor to the next page.
     */
    @ApiMethod(    httpMethod = "GET",    name = "listtest.list",     path = "ch/list")
    @SuppressWarnings({ "unchecked", "unused" })
    public CollectionResponse<Test> listTest(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

        EntityManager mgr = null;
        Cursor cursor = null;
        List<Test> execute = null;

        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select x from Test x");
            if (cursorString != null && cursorString != "") {
                cursor = Cursor.fromWebSafeString(cursorString);
                query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
            }

            if (limit != null) {
                query.setFirstResult(0);
                query.setMaxResults(limit);
            }

            execute = (List<Test>) query.getResultList();
            cursor = JPACursorHelper.getCursor(execute);
            if (cursor != null)
                cursorString = cursor.toWebSafeString();

            // Tight loop for fetching all entities from datastore and accomodate
            // for lazy fetch.
            for (Test obj : execute);
        } 
        finally 
        {
            mgr.close();
        }

            return CollectionResponse.<Test> builder().setItems(execute).setNextPageToken       (cursorString).build();
    }

    private boolean containsCodeheader(Test test) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            Test item = mgr
                    .find(Test.class, test.getCodeid());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

        private static EntityManager getEntityManager() {
        return EMF.get().createEntityManager();
    }

    }
<?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_2_0.xsd" version="2.0">

    <persistence-unit name="transactions-optional" transaction-type="RESOURCE_LOCAL">
        <provider></provider>
        <class>com.testApp.Test</class>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
端点类如下所示:

CREATE Table Test(codeid varchar(3) NOT NULL,codedesc varchar(20) NOT NULL,PRIMARY KEY (codeid));
 import java.io.Serializable;
    import javax.persistence.*;



    @Entity
    public class Test implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private String codeid;

    private String codedesc;

    public Test() {
    }

    public String getCodeid() {
    return this.codeid;
    }

    public void setCodeid(String codeid) {
this.codeid = codeid;
    }

    public String getCodedesc() {
        return this.codedesc;
    }

    public void setCodedesc(String codedesc) {
        this.codedesc = codedesc;
    }
    }
 @Api(name = "testendpoint" , version = "v1")
    public class TestEndpoint {

    /**
     * This method lists all the entities inserted in datastore.
     * It uses HTTP GET method and paging support.
     *
     * @return A CollectionResponse class containing the list of all entities
     * persisted and a cursor to the next page.
     */
    @ApiMethod(    httpMethod = "GET",    name = "listtest.list",     path = "ch/list")
    @SuppressWarnings({ "unchecked", "unused" })
    public CollectionResponse<Test> listTest(
            @Nullable @Named("cursor") String cursorString,
            @Nullable @Named("limit") Integer limit) {

        EntityManager mgr = null;
        Cursor cursor = null;
        List<Test> execute = null;

        try {
            mgr = getEntityManager();
            Query query = mgr.createQuery("select x from Test x");
            if (cursorString != null && cursorString != "") {
                cursor = Cursor.fromWebSafeString(cursorString);
                query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
            }

            if (limit != null) {
                query.setFirstResult(0);
                query.setMaxResults(limit);
            }

            execute = (List<Test>) query.getResultList();
            cursor = JPACursorHelper.getCursor(execute);
            if (cursor != null)
                cursorString = cursor.toWebSafeString();

            // Tight loop for fetching all entities from datastore and accomodate
            // for lazy fetch.
            for (Test obj : execute);
        } 
        finally 
        {
            mgr.close();
        }

            return CollectionResponse.<Test> builder().setItems(execute).setNextPageToken       (cursorString).build();
    }

    private boolean containsCodeheader(Test test) {
        EntityManager mgr = getEntityManager();
        boolean contains = true;
        try {
            Test item = mgr
                    .find(Test.class, test.getCodeid());
            if (item == null) {
                contains = false;
            }
        } finally {
            mgr.close();
        }
        return contains;
    }

        private static EntityManager getEntityManager() {
        return EMF.get().createEntityManager();
    }

    }
<?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_2_0.xsd" version="2.0">

    <persistence-unit name="transactions-optional" transaction-type="RESOURCE_LOCAL">
        <provider></provider>
        <class>com.testApp.Test</class>
        <properties>
            <property name="datanucleus.NontransactionalRead" value="true"/>
            <property name="datanucleus.NontransactionalWrite" value="true"/>
            <property name="datanucleus.ConnectionURL" value="appengine"/>
如果你需要更多信息,请告诉我

我已经进行了进一步的测试,发现我上面的示例适用于我以前从头创建的另一个测试应用程序引擎项目。我发现的一个不同之处是,当我在本地运行已损坏的应用程序引擎时,我在控制台窗口中得到以下警告,而在工作的测试应用程序中没有:


备份存储\war\WEB-INF\appengine生成的\local\u db.bin不存在。它将被创建。

我很高兴地报告,我找到了这个问题的答案。结果证明我做的每件事都是正确的。我唯一要做的就是从persistence.xml文件中删除以下行:

<property name="datanucleus.ConnectionURL" value="appengine"/>

我已经在文件中设置了以下内容:

<property name="javax.persistence.jdbc.driver" value="com.google.appengine.api.rdbms.AppEngineDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms://Your db connection/databasename"/>.

.

您说您正在使用EclipseLink,但在其中放入了各种DataNucleus属性(这些属性不适用于“云SQL”)。建议您决定使用哪个JPA实现并相应地设置属性。感谢DataNucleus的回复。我已删除了此问题上的EclipseLink标记。我的persistence.xml文件是根据google安装说明创建的,并且包含云sql连接详细信息等。为什么要删除“EclipseLink”标记是否将EclipseLink用作JPA提供者?如果persistence.xml包含EclipseLink连接详细信息,那么为什么不发布真实的连接详细信息,而不是来自GAE/Datastore usage的连接详细信息?