使用谷歌应用程序引擎与JPA和云SQL不返回任何记录使用端点 [英] using google app engine with JPA and cloud sql not returning any records using endpoints

查看:130
本文介绍了使用谷歌应用程序引擎与JPA和云SQL不返回任何记录使用端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目和一个应用程序引擎连接的项目。我正在使用以下内容:
JPA v2,
App Engine 1.7.6,
Java 1.7编译器,
Eclipse 4.2 Juno,
EclipseLink 2.4.x



我正在使用Cloud sql db。我可以在JPA和DB Persective窗口中成功连接,查询数据返回正常。我已经建立了我的应用程序引擎,以便将SQL开发连接到我的CLOUD Sql db。



我有一个表定义如下:

  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实现Serializable {
private static final long serialVersionUID = 1L;

@Id
private String codeid;

private String codedesc;
$ b $ 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 {

/ **
*此方法列出了插入数据存储中的所有实体。
*它使用HTTP GET方法和分页支持。
*
* @return包含所有实体
*的列表的CollectionResponse类将保持不变,并将光标移至下一页。
* /
@ApiMethod(httpMethod =GET,name =listtest.list,path =ch / list)
@SuppressWarnings({unchecked,unused })
public CollectionResponse< Test> listTest(
@Nullable @Named(cursor)字符串cursorString,
@Nullable @Named(limit)整数限制){

EntityManager mgr = null;
游标cursor = null;
List< Test> execute = null;

尝试{
mgr = getEntityManager();
Query query = mgr.createQuery(Test 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();

//用于从数据存储中获取所有实体的紧密循环,并容纳
//用于懒惰获取。
for(Test obj:execute);
}
finally
{
mgr.close();
}

返回CollectionResponse。<测试> 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();
}
返回包含;


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

}

persistence.xml如下所示:

 <?xml version =1.0encoding =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版本= 2.0 >

< persistence-unit name =transactions-optionaltransaction-type =RESOURCE_LOCAL>
< provider>< / provider>
< class> com.testApp.Test< / class>
<属性>
< property name =datanucleus.NontransactionalReadvalue =true/>
< property name =datanucleus.NontransactionalWritevalue =true/>
< property name =datanucleus.ConnectionURLvalue =appengine/>

我正在尝试运行我的端点以获取记录列表。我在运行以下命令时不会在控制台中出现任何错误。



localhost:8888 / _ah / api / testendpoint / v1 / ch / list



当我知道我的表中有记录时,我在Google Chrome浏览器中查看。

c> {
items:[]
}

请让我知道您是否需要更多信息。



我进行了进一步测试,发现我的上述示例适用于之前从头创建的另一个测试应用程序引擎项目。我发现的一个区别是,当我在本地运行中断的应用程序引擎时,在控制台窗口中显示以下警告,我不在工作测试应用程序中:

存储\war\WEB-INF\appengine-generated\local_db.bin,不存在。它将被创建。

解决方案

我很高兴地报告我找到了这个问题的答案。原来我正在做的一切正确。我唯一需要做的是从persistence.xml文件中取出以下代码:

 < property name =datanucleus .ConnectionURLvalue =appengine/> 

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

 < property name =javax.persistence.jdbc.drivervalue =com.google.appengine.api.rdbms.AppEngineDriver/> 
< property name =javax.persistence.jdbc.urlvalue =jdbc:google:rdbms://您的db连接/ databasename/> ;.


I have an Android project and an app engine connected project. I am using the following: JPA v2, App Engine 1.7.6, Java 1.7 compiler, Eclipse 4.2 Juno, EclipseLink 2.4.x

I am using Cloud sql db. I am able to connect successfully in the JPA and DB Persective window and query data back ok. I have set up my app engine to have the SQL Development to be connected to my CLOUD Sql db.

I have one table defined as follows:

CREATE Table Test(codeid varchar(3) NOT NULL,codedesc varchar(20) NOT NULL,PRIMARY KEY (codeid));

The Entity class is as follows:

 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;
    }
    }

the endpoint class is as follows:

 @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();
    }

    }

persistence.xml looks as follows:

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

What I am trying to do is run my endpoint to get a list of records back . I When I run the following I dont get any errors in the console.

localhost:8888/_ah/api/testendpoint/v1/ch/list

I get the follwoing in the Google Chrome when I know there are records in my table.

 {
    "items" : [ ]
  }

Please let me know if you need more info.

I have carried out further testing and found my above example works for another test app engine project I created before from scratch. A difference I have found is, when I run the broken app engine locally I get the following warning in the Console window which I dont in the working test app:

The backing store, \war\WEB-INF\appengine-generated\local_db.bin, does not exist. It will be created.

解决方案

Well I am happy to report that I found the answer to this issue. Turns out I was doing everything correctly. The only thing I had to do was take the following line out of persistence.xml file:

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

I already had the following set in the file:

<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"/>.

这篇关于使用谷歌应用程序引擎与JPA和云SQL不返回任何记录使用端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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