Objectify - App Engine存储列表

时间:2012-10-15 15:13:02

标签: google-app-engine objectify

我想在我的实体List<P>中使用P

@Entity
@Index
public class P {
    @Id Long id;
    String email;
    List<P> list = new ArrayList<P>();
}

但是每次我插入新实体时,我都可以在数据存储区查看器中看到emailid,但没有list:/列表是否有任何特殊功能?

3 个答案:

答案 0 :(得分:2)

您的P实体中不能有一个P列表用于客观化。不存在任何循环依赖。

您还需要使用@Embed关键字在实体中嵌入实体。看这里:http://code.google.com/p/objectify-appengine/wiki/Entities#Embedded_Collections_and_Arrays

答案 1 :(得分:2)

我的建议是分别存储每个实体P,并仅保留对其“包含”的其他P实体的引用。所以,你可能会遇到以下情况:

@Entity
@Index
public class P {
    @Id Long id;
    String email;
    List<Key<P>> list = new ArrayList<Key<P>>();
}

这样,您仍然可以访问所有“子”实体,但没有主P实体内的所有信息。

希望这有帮助!

答案 2 :(得分:0)

更详细的答案:

package com.netcomps.objectify_test;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Parent;
import com.googlecode.objectify.Key;

import java.lang.String;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Entity
public class User {
  @Parent Key<World> theWorld;
  @Id public Long id;

  public String email;
  public String name;
  @Index public Date date;
  //public ArrayList<User> friends;
  List<Key<User>> friends;

  /**
   * Simple constructor just sets the date
   **/
  public User() {
    date = new Date();
  }

  /**
   * A connivence constructor
   **/
  public User(String world, String name) {
    this();
    if( world != null ) {
        theWorld = Key.create(World.class, world);  // Creating the Ancestor key
    } else {
        theWorld = Key.create(World.class, "default");
    }
    this.name = name;
  }

  /**
   * Takes all important fields
   **/
  public User(String book, String content, String email) {
    this(book, content);
    this.email = email;
  }


  public void addFriend(User friend) {
      if (friends == null) {
          friends = new ArrayList<Key<User>>();
      }
      Key<User> f = Key.create(theWorld, User.class, friend.id);
      friends.add(f);
  }


}