来自实例化但未存储的实体的访问密钥 [objectify] [@prepersist]
我编写了一个 @PrePersist 方法,该方法在保留我的 User 对象之前调用。该方法的目的是为用户进行一种预订,因此其他人无法获取他的电子邮件地址。因此,当用户即将被保留时,我说“为用户#xyz 创建一个 EmailReservation”。
我的麻烦是当用户之前没有被持久化时获取#xyz。 id 为 null,但我无法使用 DatastoreService.allocateIds,因为代表我的用户的实体已经创建。我确实可以访问该实体,但 getKey() 返回一个带有 null id 的键 - 它尚未决定其 id 是什么。
因此,我希望以下其中一项是可能的:
- 找出实体在持久化时将具有的 id
- 在持久化之前为已创建的实体提供特定的 id。
有什么想法吗?
I have written a @PrePersist method that's called before my User object is persisted. The purpose of the method is to make a kind of reservation for the User, so no one else can take his email address. So, when the User is about to be persisted, I say, "create an EmailReservation for user #xyz."
My trouble is in getting the #xyz when the User has not been persisted before. The id is null, but I cannot use DatastoreService.allocateIds because the Entity that will represent my User has already been made. I do have access to that Entity, but getKey() returns a key with a null id - it hasn't yet decided what its id will be.
So, I'm hoping one of the following is possible:
- Figure out the id that an Entity will have when it's persisted
- Give an Entity that's already been created a specific id before it's persisted.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有办法知道实体在持久化时将拥有什么 ID;唯一的方法是使用 allocateIds。
您必须在创建 User 对象之前生成 ID,将其传入,或者在将实体写入数据存储区后进行工作。
There's no way to know what ID an entity will have when it's persisted; the only way to do so is to use allocateIds.
You will have to either generate an ID before creating the User object, passing it in, or do your work after the entity has been written to the datastore.