我有一个Android项目和一个app引擎连接项目。我使用以下内容: JPA v2, App Engine 1.7.6, Java 1.7编译器, Eclipse 4.2 Juno, EclipseLink 2.4.x
我正在使用Cloud sql db。我能够在JPA和DB Persective窗口中成功连接并查询数据。我已经设置了我的app引擎,以便将SQL Development连接到我的CLOUD Sql db。
我有一个表定义如下:
CREATE Table Test(codeid varchar(3) NOT NULL,codedesc varchar(20) NOT NULL,PRIMARY KEY (codeid));
Entity类如下:
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();
}
}
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_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"/>
我要做的是运行我的端点以获取记录列表。 I当我运行以下内容时,我在控制台中没有出现任何错误。
本地主机:8888 / _ah / API / testendpoint / V1 / CH /列表
当我知道桌面上有记录时,我会在谷歌浏览器中接下来。
{
"items" : [ ]
}
如果您需要更多信息,请与我们联系。
我已经进行了进一步的测试,发现我的上述示例适用于我从头开始创建的另一个测试应用引擎项目。我发现的一个区别是,当我在本地运行损坏的应用程序引擎时,我在控制台窗口中收到以下警告,我在工作测试应用程序中没有:
后备存储\ war \ WEB-INF \ appengine-generated \ local_db.bin不存在。它将被创建。
答案 0 :(得分:1)
我很高兴地报告我找到了这个问题的答案。结果我正在做的一切正确。我唯一要做的就是从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"/>.