Problem with collections in GAE
Learning GAE I`ve got some issue. I want to save contact list of my users in database. I use addUser() to add new user and it works. But when I refresh the page the list is empty (not null). In GAE "Datastore Viewer" the "list" field is null but in application it exists.
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class ContactList implements IUserList {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
protected List<Key> list;
@Persistent(mappedBy = "contactList")
private User user;
public Key getKey(){
return this.key;
}
public User getUser() {
return user;
}
public ContactList(User user) {
this.list = new ArrayList<Key>(); //this line invoice one only
}
@Override
public Boolean addUser(User user) {
if(th开发者_运维知识库is.list.contains(user.getKey())){
return false;
}else{
this.list.add(user.getKey());
return true;
}
}
@Override
public boolean containsKey(Long id) {
return false; //this.list.containsKey(id);
}
@Override
public User get(Long id) {
return null;// this.list.get(id);
}
@Override
public void removeUser(User user) {
this.list.remove(user.getKey());
}
@Override
public int size() {
return this.list.size();
}
@Override
public List<User> users() {
ArrayList<User> users = new ArrayList<User>();
for(Key key : this.list) {
User user = UserManager.getInstance().getUser(key);
users.add(user);
}
return users;
}
@Override
public void addEvent(IEvent event) {
for(User user : this.users()) {
user.addEvent(event);
}
}
@Override
public void clearEvents() {
}
@Override
public ArrayList<IEvent> getEvents() {
return null;
}
}
You're not persisting your data to your database, so you're actually performing ghost reads.
Ways to save your data: http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html
精彩评论