每个用户可能有很多问题。问题只能有一个用户。以下正确的是
Key questionKey = KeyFactory.createKey("Questions", userId);
Entity questionEntity = new Entity("Question", questionKey);
questionEntity.setProperty("questionCategory", questionCategory);
...
最佳答案
给定的用法是错误的。对于这个问题,您正在使用 kind 和 userid 创建 key 。这意味着相应的实体是 Kind="Questions"
和 id=userid
并且没有父级。这是错误的,一旦您向某个用户提出超过 1 个问题,您就会开始收到错误,因为它们都具有相同的 key 。
理想情况下,对于问题实体,将其类型声明为问题,并将其父级声明为用户,如下所示:
1,如果问题使用手动生成的 ID 或名称,则:
Key questionKey = KeyFactory.createKey(userkey, "Questions", questionidorname);
2,如果使用app引擎自动生成id,则无需创建 key ,而是创建实体:
Entity questionEntity = new Entity("Questions",
userkey)
这里userkey是用户实体的 key
关于java - KeyFactory.createKey() 这是在数据存储中存储数据的正确方法吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17592684/