簡體   English   中英

數據在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'
}

到目前為止,我可以在谷歌雲上訪問我的appspot應用,可以從瀏覽器進行JSON查詢,沒問題,但是當我檢查索引時,即:

App Engine后端管理員默認本地鏈接

我沒有看到任何索引:

打印屏幕

為什么會這樣呢? 我應該對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();
}
}

我正在關注本教程

有任何想法嗎?

我讀過某個地方,我應該在后端上有一個datastore-indexes.xml文件,但是我沒有任何文件,應該創建一個嗎?

提前致謝!

這里有兩個問題:

1)默認情況下,Objectify不會索引字段(不統一JDO或JPA),因為索引不必要的字段會增加您的寫入成本,而沒有任何好處。 為了索引字段,您需要在實體類中需要索引的字段中添加@Index。 添加@index時會自動創建單個字段索引,該索引不會顯示在索引列表中,而是在按單個字段進行過濾/排序時使用。

2)可以手動創建多屬性索引(列表中顯示的索引),也可以使用datastore-indexes.xml上的自動生成索引設置來創建。 僅當您一次查詢/排序多個字段時,此索引才起作用。 希望這可以澄清。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM