有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java数据在Google App Engine Objectify Android上不持久

我正在我的Android项目上实现一个Google应用程序引擎后端

到目前为止,我在我的模块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'
}

到目前为止,我可以在谷歌云上访问我的appspot应用程序,我可以从浏览器进行JSON查询,没有问题,但当我检查索引时,即:

app engine backend admin default local link

我没有看到任何索引:

Printscreen

为什么会这样?我应该用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文件,但我没有,我应该创建一个吗

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    这里有两个问题:

    1)Objectify默认情况下不会索引字段(取消链接JDO或JPA),因为索引不必要的字段会增加您的写入成本,而不会带来任何好处。为了索引字段,您需要将@index添加到需要在实体类中索引的字段中。添加@index时会自动创建单字段索引,该索引不会显示在索引列表中,而是在按单个字段进行筛选/排序时使用

    2)多属性索引(列表中显示的索引)可以手动创建,也可以使用数据存储索引上的自动生成索引设置创建。xml。只有在一次查询/排序多个字段时,此索引才会起作用。 希望这能澄清