Objectify 中的嵌入实体似乎不会自动生成 ID
我在我的第一个严肃的 Google App Engine 项目中使用了 Objectify,总体来说一切进展顺利(多么可爱的库啊!)。不幸的是,我在保留我的实体时遇到了问题。
我的基本结构如下:
@Entity
class Parent {
@Id
long id = 123;
@Embedded
Child[] children;
}
@Entity
class Child {
@Id
Long id;
}
我手动声明父实体的id,但我希望嵌入的子实体自动生成id。我确实想知道是否完全删除子实体中的 @Id 并围绕它进行编码,但随后我收到了有关需要 @Id 的实体的错误。
有人可以帮忙吗?我使用 id 是为了比较父实体的子实体之间的更改,因此它对于我当前的设计方式来说相当基础。如果这是一个更好的解决方案,我可以重新架构。
I'm using Objectify with my first serious Google App Engine project, and generally it's all going swimmingly (what a lovely library!). Unfortunately, I've come across a problem when persisting my entities.
My basic structure is as follows:
@Entity
class Parent {
@Id
long id = 123;
@Embedded
Child[] children;
}
@Entity
class Child {
@Id
Long id;
}
I am manually declaring the ids of the parent entities, but I want the embedded child entities to automatically generate an id. I did wonder about just removing the @Id in the child entity entirely and coding around it, but then I get errors about entities needing the @Id.
Can someone please help? I am using the id, in order to compare changes between parent entities' children, so it's fairly fundamental to the current way I've designed it. I can re-architect though, if that would be a better solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 http://code.google.com/p/objectify-appengine/ wiki/IntroductionToObjectify#Entity_Representation ,它看起来 @Embedded 实体实际上根本不是数据存储中的单独实体。它们只是作为属性直接存储在父对象的字段中。
如果您希望子实体成为它们自己的、可单独访问的对象,则应更改
Parent
以保存 Key 或 ID 数组,然后单独实例化您的Child
对象。这将为每个 Child 生成 id(您可以将其存储在Parent
的 id 数组中)。From http://code.google.com/p/objectify-appengine/wiki/IntroductionToObjectify#Entity_Representation , it doesn't look like @Embedded entities are actually separate entities in your datastore at all. They're just stored as properties directly in fields of the parent object.
If you want the Child entities to be their own, separately-accessible objects, you should change
Parent
to hold an array of Keys or IDs, and then instantiate yourChild
objects separately. That'll generate ids for each Child (which you can store in the id array in theParent
).