【问题标题】:How to get an Entity for which a users key is held in?如何获取保存用户密钥的实体?
【发布时间】:2016-03-16 14:33:53
【问题描述】:

所以基本上我想得到一个人所在的商店?这是商店的 Pojo 代码;

@Entity
public class Store{

    @Id
    Long StoreId;
    String Name;
    String Address;

  @Index  List<Key<Person>> Members;


    private House() {
    }

    public Store(Long StoreId ,String Name, String Address) {
        StoreId = StoreId;
        this.Name = Name;
        this.Address = Address;
        Members = new ArrayList<Key<Person>>();

    }



    public Long getId() {
        return StoreId;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        this.Name = name;
    }

    public String getAddress() {
        return Address;
    }

    public void setAddress(String postcode) {
        this.Address = Address;
    }



    public void AddToStore(Key<Person> person) {
        Members.add(person);


    }

这是创建商店并添加当前登录用户(Google 登录)的代码

/**
     * Inserts a new {@code Store}.
     */
    @ApiMethod(
            name = "insert",
            path = "Store",
            httpMethod = ApiMethod.HttpMethod.POST)
    public House insert(User user) throws UnauthorizedException {


        if (user == null){
            throw new UnauthorizedException("You need to authorisation");

        }
        String tempName = "cottages";
        String tempAddress = "Gogoland";
        String userId = user.getUserId();


        Key<Person> personKey = Key.create(Person.class, userId);
        final Key<Store> storeKey = OfyService.factory().allocateId(Store.class);
        final Long StoreId = storeKey.getId();
         Store h = new Store(StoreId , tempName,tempAddress);
        h.AddToStore(personKey);

        ofy().save().entity(h).now();
        logger.info("Created Store.");

        return ofy().load().entity(h).now();
    }

所以如您所见,Store 与 person 之间的关系是一对多的。所以我希望能够获取登录用户的当前房屋,然后检索对象中的其他“人”/成员实体。此外,我还希望在与当前用户相同的商店中检索“Person”实体的“孩子”。

此外,如何通过电子邮件邀请其他人加入用户商店?

感谢任何帮助。

【问题讨论】:

  • 修改了问题,使其更清晰。非常感谢任何帮助:-)

标签: java android google-app-engine google-cloud-datastore objectify


【解决方案1】:

你应该按键过滤,因为你有一个索引:

return ofy().load().filter("Members", personKey).first().now();

但是,如果您已经使用 .save() 方法保存了实体,您可以简单地这样做:

return h;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 2012-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-01
    • 1970-01-01
    相关资源
    最近更新 更多