我有一个 Android 项目和一个应用引擎连接项目。我正在使用以下产品:JPA v2、App Engine 1.7.6、Java 1.7 编译器、Eclipse 4.2 Juno、EclipseLink 2.4.x
我正在使用云 sql 数据库。我能够在 JPA 和 DB Persective 窗口中成功连接并正常查询数据。我已经设置了我的应用程序引擎,以便将 SQL 开发连接到我的 CLOUD 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();
}
}
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"/>
我想要做的是运行我的端点以获取记录列表。我当我运行以下命令时,控制台中没有任何错误。
本地主机:8888/_ah/api/testendpoint/v1/ch/list
当我知道我的表中有记录时,我会在 Google Chrome 中得到以下信息。
{
"items" : [ ]
}
如果您需要更多信息,请告诉我。
我进行了进一步的测试,发现上面的示例适用于我之前从头开始创建的另一个测试应用程序引擎项目。我发现的一个区别是,当我在本地运行损坏的应用程序引擎时,我在控制台窗口中收到以下警告,而我在工作测试应用程序中没有:
后备存储 \war\WEB-INF\appengine-generated\local_db.bin 不存在。它将被创建。