数据在 Google App Engine 上不持久 - Objectify - Android
Data not persistent on Google App Engine - Objectify - Android
我正在我的 Android 项目中实施 Google App Engine 后端。
到目前为止我在我的模块上使用它 gradle:
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
compile 'com.google.appengine:appengine-endpoints:1.9.18'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
compile 'com.googlecode.objectify:objectify:5.0.3'
compile 'javax.servlet:servlet-api:2.5'
}
到目前为止,我可以在 google 云上访问我的 appspot 应用程序,我可以从浏览器进行 JSON 查询,没问题,但是当我检查索引时,即:
app engine backend admin default local link
我没有看到任何索引:
为什么会这样?我应该用 Objectify
库做更多的事情吗?
这是我的文件声明(User.java):
package com.kkoci.shairlook.backend;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
/**
* Created by kristian on 01/07/2015.
*/
@Entity
public class User {
@Id
Long id;
String who;
String what;
String email;
String password;
String school;
public User() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getWhat() {
return what;
}
public void setWhat(String what) {
this.what = what;
}
}
这是我的端点代码 (UserEndPoint.java):
package com.kkoci.shairlook.backend;
import com.kkoci.shairlook.backend.User;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.config.Nullable;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.api.server.spi.response.ConflictException;
import com.google.api.server.spi.response.NotFoundException;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.cmd.Query;
import static com.kkoci.shairlook.backend.OfyService.ofy;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Named;
/**
* Created by kristian on 01/07/2015.
*/
@Api(name = "userEndpoint", version = "v1", namespace = @ApiNamespace(ownerDomain = "shairlook1.appspot.com", ownerName = "shairlook1.appspot.com", packagePath=""))
public class UserEndPoint {
// Make sure to add this endpoint to your web.xml file if this is a web application.
public UserEndPoint() {
}
/**
* Return a collection of users
*
* @param count The number of users
* @return a list of Users
*/
@ApiMethod(name = "listUser")
public CollectionResponse<User> listUser(@Nullable @Named("cursor") String cursorString,
@Nullable @Named("count") Integer count) {
Query<User> query = ofy().load().type(User.class);
if (count != null) query.limit(count);
if (cursorString != null && cursorString != "") {
query = query.startAt(Cursor.fromWebSafeString(cursorString));
}
List<User> records = new ArrayList<User>();
QueryResultIterator<User> iterator = query.iterator();
int num = 0;
while (iterator.hasNext()) {
records.add(iterator.next());
if (count != null) {
num++;
if (num == count) break;
}
}
//Find the next cursor
if (cursorString != null && cursorString != "") {
Cursor cursor = iterator.getCursor();
if (cursor != null) {
cursorString = cursor.toWebSafeString();
}
}
return CollectionResponse. <User>builder().setItems(records).setNextPageToken(cursorString).build();
}
/**
* This inserts a new <code>User</code> object.
* @param user The object to be added.
* @return The object to be added.
*/
@ApiMethod(name = "insertUser")
public User insertUser(User user) throws ConflictException {
//If if is not null, then check if it exists. If yes, throw an Exception
//that it is already present
if (user.getId() != null) {
if (findRecord(user.getId()) != null) {
throw new ConflictException("Object already exists");
}
}
//Since our @Id field is a Long, Objectify will generate a unique value for us
//when we use put
ofy().save().entity(user).now();
return user;
}
/**
* This updates an existing <code>User</code> object.
* @param user The object to be added.
* @return The object to be updated.
*/
@ApiMethod(name = "updateUser")
public User updateUser(User user)throws NotFoundException {
if (findRecord(user.getId()) == null) {
throw new NotFoundException("User Record does not exist");
}
ofy().save().entity(user).now();
return user;
}
/**
* This deletes an existing <code>User</code> object.
* @param id The id of the object to be deleted.
*/
@ApiMethod(name = "removeUser")
public void removeUser(@Named("id") Long id) throws NotFoundException {
User record = findRecord(id);
if(record == null) {
throw new NotFoundException("User Record does not exist");
}
ofy().delete().entity(record).now();
}
//Private method to retrieve a <code>User</code> record
private User findRecord(Long id) {
return ofy().load().type(User.class).id(id).now();
//or return ofy().load().type(User.class).filter("id",id).first.now();
}
}
还有我的 OfyService
(OfyService.java) 代码:
package com.kkoci.shairlook.backend;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
/**
* Created by kristian on 01/07/2015.
*
* Objectify service wrapper so we can statically register our persistence classes
* More on Objectify here : https://code.google.com/p/objectify-appengine/
*
*/
public class OfyService {
static {
ObjectifyService.register(User.class);
}
public static Objectify ofy() {
return ObjectifyService.ofy();
}
public static ObjectifyFactory factory() {
return ObjectifyService.factory();
}
}
我正在关注这个 Tutorial
有什么想法吗?
我在某处读到我的后端应该有一个 datastore-indexes.xml
文件,但我没有,我应该创建一个吗?
提前致谢!
这里有 2 个问题:
1) 默认情况下 Objectify 不会索引字段(与 JDO 或 JPA 不同),因为索引不必要的字段会增加您的写入成本而没有任何好处。为了索引字段,您需要将@Index 添加到您需要在实体class 中索引的字段。添加@index时会自动创建单个字段索引,该索引不会显示在索引列表中,当您filter/sort被单个字段使用时使用。
2) 多个 属性 索引(列表中显示的索引)可以手动创建,也可以使用数据存储上的自动生成索引设置创建 -indexes.xml 。只有当您一次 query/sort 多个字段时,该索引才会发挥作用。
希望这能澄清。
我正在我的 Android 项目中实施 Google App Engine 后端。
到目前为止我在我的模块上使用它 gradle:
dependencies {
appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.18'
compile 'com.google.appengine:appengine-endpoints:1.9.18'
compile 'com.google.appengine:appengine-endpoints-deps:1.9.18'
compile 'com.googlecode.objectify:objectify:5.0.3'
compile 'javax.servlet:servlet-api:2.5'
}
到目前为止,我可以在 google 云上访问我的 appspot 应用程序,我可以从浏览器进行 JSON 查询,没问题,但是当我检查索引时,即:
app engine backend admin default local link
我没有看到任何索引:
为什么会这样?我应该用 Objectify
库做更多的事情吗?
这是我的文件声明(User.java):
package com.kkoci.shairlook.backend;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
/**
* Created by kristian on 01/07/2015.
*/
@Entity
public class User {
@Id
Long id;
String who;
String what;
String email;
String password;
String school;
public User() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getWho() {
return who;
}
public void setWho(String who) {
this.who = who;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getWhat() {
return what;
}
public void setWhat(String what) {
this.what = what;
}
}
这是我的端点代码 (UserEndPoint.java):
package com.kkoci.shairlook.backend;
import com.kkoci.shairlook.backend.User;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.config.Nullable;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.api.server.spi.response.ConflictException;
import com.google.api.server.spi.response.NotFoundException;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.cmd.Query;
import static com.kkoci.shairlook.backend.OfyService.ofy;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.inject.Named;
/**
* Created by kristian on 01/07/2015.
*/
@Api(name = "userEndpoint", version = "v1", namespace = @ApiNamespace(ownerDomain = "shairlook1.appspot.com", ownerName = "shairlook1.appspot.com", packagePath=""))
public class UserEndPoint {
// Make sure to add this endpoint to your web.xml file if this is a web application.
public UserEndPoint() {
}
/**
* Return a collection of users
*
* @param count The number of users
* @return a list of Users
*/
@ApiMethod(name = "listUser")
public CollectionResponse<User> listUser(@Nullable @Named("cursor") String cursorString,
@Nullable @Named("count") Integer count) {
Query<User> query = ofy().load().type(User.class);
if (count != null) query.limit(count);
if (cursorString != null && cursorString != "") {
query = query.startAt(Cursor.fromWebSafeString(cursorString));
}
List<User> records = new ArrayList<User>();
QueryResultIterator<User> iterator = query.iterator();
int num = 0;
while (iterator.hasNext()) {
records.add(iterator.next());
if (count != null) {
num++;
if (num == count) break;
}
}
//Find the next cursor
if (cursorString != null && cursorString != "") {
Cursor cursor = iterator.getCursor();
if (cursor != null) {
cursorString = cursor.toWebSafeString();
}
}
return CollectionResponse. <User>builder().setItems(records).setNextPageToken(cursorString).build();
}
/**
* This inserts a new <code>User</code> object.
* @param user The object to be added.
* @return The object to be added.
*/
@ApiMethod(name = "insertUser")
public User insertUser(User user) throws ConflictException {
//If if is not null, then check if it exists. If yes, throw an Exception
//that it is already present
if (user.getId() != null) {
if (findRecord(user.getId()) != null) {
throw new ConflictException("Object already exists");
}
}
//Since our @Id field is a Long, Objectify will generate a unique value for us
//when we use put
ofy().save().entity(user).now();
return user;
}
/**
* This updates an existing <code>User</code> object.
* @param user The object to be added.
* @return The object to be updated.
*/
@ApiMethod(name = "updateUser")
public User updateUser(User user)throws NotFoundException {
if (findRecord(user.getId()) == null) {
throw new NotFoundException("User Record does not exist");
}
ofy().save().entity(user).now();
return user;
}
/**
* This deletes an existing <code>User</code> object.
* @param id The id of the object to be deleted.
*/
@ApiMethod(name = "removeUser")
public void removeUser(@Named("id") Long id) throws NotFoundException {
User record = findRecord(id);
if(record == null) {
throw new NotFoundException("User Record does not exist");
}
ofy().delete().entity(record).now();
}
//Private method to retrieve a <code>User</code> record
private User findRecord(Long id) {
return ofy().load().type(User.class).id(id).now();
//or return ofy().load().type(User.class).filter("id",id).first.now();
}
}
还有我的 OfyService
(OfyService.java) 代码:
package com.kkoci.shairlook.backend;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyFactory;
import com.googlecode.objectify.ObjectifyService;
/**
* Created by kristian on 01/07/2015.
*
* Objectify service wrapper so we can statically register our persistence classes
* More on Objectify here : https://code.google.com/p/objectify-appengine/
*
*/
public class OfyService {
static {
ObjectifyService.register(User.class);
}
public static Objectify ofy() {
return ObjectifyService.ofy();
}
public static ObjectifyFactory factory() {
return ObjectifyService.factory();
}
}
我正在关注这个 Tutorial
有什么想法吗?
我在某处读到我的后端应该有一个 datastore-indexes.xml
文件,但我没有,我应该创建一个吗?
提前致谢!
这里有 2 个问题:
1) 默认情况下 Objectify 不会索引字段(与 JDO 或 JPA 不同),因为索引不必要的字段会增加您的写入成本而没有任何好处。为了索引字段,您需要将@Index 添加到您需要在实体class 中索引的字段。添加@index时会自动创建单个字段索引,该索引不会显示在索引列表中,当您filter/sort被单个字段使用时使用。
2) 多个 属性 索引(列表中显示的索引)可以手动创建,也可以使用数据存储上的自动生成索引设置创建 -indexes.xml 。只有当您一次 query/sort 多个字段时,该索引才会发挥作用。 希望这能澄清。