所以基本上我想得到一个人所在的商店?这是商店的 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”实体的“children”。
此外,如何通过电子邮件邀请其他人加入用户商店?
感谢任何帮助。
最佳答案
你应该按键过滤,因为你有一个索引:
return ofy().load().filter("Members", personKey).first().now();
但是,如果您已经使用 .save() 方法保存了实体,您可以简单地这样做:
return h;
关于java - 如何获取保存用户 key 的实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038717/