【发布时间】:2017-02-05 21:58:55
【问题描述】:
我想在我的应用中实现友谊结构。我的第一种方法是创建具有以下结构的友谊实体
@Entity Friendship{
@Id Long id;
@Parent User<Key> userA;
@Index User<Ref> userB;
}
每个友谊都由两个友谊实体表示,每个用户一个。这适用于简单的情况,但现在我想运行查询find user's friends with name foo,所以我在友谊中添加@Index String userBName,并在创建实体时设置它。但是每次 userB 更改他的名字时都需要更新这个值,而且它是索引中的附加列,这是我想要避免的事情。
所以我想出了另一种方法:删除userB 属性并将Friendship id 设置为@Id 的userB 引用的用户。然后我将能够查询具有给定名称的用户,基于它的@Id 创建密钥并按父级过滤友谊并创建密钥。
@Entity Friendship{
@Id Long id;
@Parent User<Key> userA;
}
Key<User> userA;
Key<User> userB;
Friendship friendshipA;
Friendship friendshipB;
CreateFriendship(){
friendshipA.setId(userB.getId());
friendshipA.setUserA(userA);
friendshipB.setId(userA.getId());
friendshipB.setUserA(userB);
}
现在我的问题是:这是手动设置实体 ID 的正确用例吗?我认为它会起作用,因为 Key 是由 parent、type 和 id 创建的。但另一方面,我认为这不会在实体之间建立逻辑联系。
这是一个好方法,还是我在创建一个反模式?
【问题讨论】:
-
您的 UserA 和 UserB 是实体组所有者(即他们没有父实体)吗?
标签: google-app-engine objectify anti-patterns