简体   繁体   中英

Data not persistent on Google App Engine - Objectify - Android

I'm implementing a Google App Engine backend on my Android project.

So far I'm using this on my module 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'
}

So far I can access my appspot app on google cloud, I can make JSON queries, from browser, no problem, but when I check indexes, ie:

app engine backend admin default local link

I don't see any indexes:

打印屏幕

Why is this happening? Should I do something more with Objectify library?

This is my document declaration (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;
}
}

This is my Endpoint code (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();
}

}

And my OfyService (OfyService.java) code:

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();
}
}

And I'm following this Tutorial

Any ideas?

I've read somewhere that I should have a datastore-indexes.xml file on my backend, but I don't have any, should I create one?

Thanks in advance!

There are 2 issues here:

1) Objectify by default does NOT index fields (unlilke JDO or JPA) because indexing unnecessary fields will raise your write costs with no benefit. In order to index fields you need to add @Index to the field you need to index in your entity class. Single field indexes are created automatically when you add @index, this indexes are not shown in the indexes list and are used when you filter/sort by a single field.

2) multi property indexes ( the ones shown on the list) are either created manually or by using the auto generate index settings on your datastore-indexes.xml . This indexes will only come in play when you query/sort on multiple fields at a time. Hope this clarifies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM