在 App Engine 上使用 JDO 保留协议缓冲区模型
我们正在构建一个 Android 应用程序,通过 Protocol Buffers 与 Java App Engine 服务器进行通信。我们使用JDO作为服务器上的持久化机制。
我的问题是如何在 App Engine 数据库中保留对象。似乎唯一的方法是为每个模型创建两个类:一个模型是从 .proto 文件生成的,第二个类包装第一个类并提供 JDO 注释、附加方法等。这看起来相当麻烦对我来说,我想知道这方面的最佳实践是什么。有没有办法避免这种重复(例如,JPA 允许在外部 XML 文件中指定持久性注释,但由于文档质量较差,我们宁愿避免在 App Engine 上使用 JPA)?
we are building an Android app that communicates with a Java App Engine Server via Protocol Buffers. We use JDO as the persistence mechanism on the server.
My question is how to persist objects in the App Engine database. It seems like the only way to do it is the create two classes for each model: one model which is generated from the .proto file and a second class that wraps the first class and provides JDO annotations, additional methods etc. This seems rather cumbersome to me and I am wondering what the best practice regarding this might be. Is there a way to avoid this duplication (JPA for instance allows to specify the persistence annotations in external XML files, but we would rather avoid JPA on App Engine because of the poor documentation)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
App Engine 的数据存储区仅存储模型协议缓冲区。如果您的客户端发送给您的协议缓冲区是一个 Model PB,您可以通过将其插入低级 API 来直接存储它。如果它是任何其他类型的 PB,您要么需要将其(手动)转换为实体,要么需要将其序列化并将其存储在实体的二进制属性中。您选择哪一个取决于您需要如何查询存储的对象。
无论哪种方式,JDO 可能都是一个糟糕的选择 - 您应该研究 Objectify,或者直接使用低级 API。
App Engine's datastore only stores Model Protocol Buffers. If the protocol buffer your client sends you is a Model PB, you could store it directly by poking it into the low level APIs. If it's any other sort of PB, you either need to convert it (manually) into an Entity, or you need to serialize it and store it in a binary property of the entity. Which of those you choose depends on how you need to query the stored object.
Either way, JDO is probably a poor choice - you should look into Objectify, or use the low-level API directly.