Google App Engine 数据存储困难

发布于 2024-12-27 05:35:30 字数 1214 浏览 1 评论 0原文

我正在使用 Google App Engine 并使用 Google 的数据库数据存储界面。 我的问题是这样的,我有以下代码:我有一个网络对象,如果它存在于数据库上,我想更新它,或者如果它是第一次,我想创建它。 。为此,我必须捕获异常并重复相同的代码两次 - 这看起来丑陋且多余,让我觉得我做错了什么。 第二件令我感到奇怪的事情是,我无法想到将对象复制到实体的方法,反之亦然。我需要自己实施吗?对每个属性使用 setProperty 或 getProperty 是非常不舒服的……我只是想知道为什么没有 objectToEntity 方法或类似的方法。

这就是我的代码目前的样子......

try {
Entity network=datastore.get(KeyFactory.stringToKey(networks.get(i)._ipDigits));
//If I get here no exception was thrown - entity already exists on db.
Network contextNet= //fetch the network object from servlet context ...
network.setProperty("ip", contextNet._ip);  //update the fields using setProperty - no better way??
network.setProperty("offlineUsers",contextNet._offlineUsers);
 datastore.put(network);

 }
//Entity doesn't exist , create a new entity and save it (while repeating the same code)...
catch (EntityNotFoundException e) {
Entity network=new Entity("network",Long.parseLong(networks.get(i)._ipDigits));

Network contextNet= // ...fetch the network object from servlet context
 network.setProperty("ip", contextNet._ip);
                        network.setProperty("offlineUsers",contextNet._offlineUsers);
datastore.put(network);


                }

I am using Google App Engine and using Google's datastore interface for a database .
My question is this , I have the following code : I have a network object that I want to either update if it exists on db , or to create if it's the first time. . For this I have to catch an exception and repeat the same code twice - it seems ugly and redundant and makes me think I'm doing something wrong .
The second thing that strikes me as odd is that there is no method I can think of that copies an object to an entity or vice versa . Am I expected to implement this myself ? It is very uncomfrotable to use the setProperty or getProperty for each property and well ...I am just wondering why there is no objectToEntity method or something of the sort.

This is how my code currently looks ...

try {
Entity network=datastore.get(KeyFactory.stringToKey(networks.get(i)._ipDigits));
//If I get here no exception was thrown - entity already exists on db.
Network contextNet= //fetch the network object from servlet context ...
network.setProperty("ip", contextNet._ip);  //update the fields using setProperty - no better way??
network.setProperty("offlineUsers",contextNet._offlineUsers);
 datastore.put(network);

 }
//Entity doesn't exist , create a new entity and save it (while repeating the same code)...
catch (EntityNotFoundException e) {
Entity network=new Entity("network",Long.parseLong(networks.get(i)._ipDigits));

Network contextNet= // ...fetch the network object from servlet context
 network.setProperty("ip", contextNet._ip);
                        network.setProperty("offlineUsers",contextNet._offlineUsers);
datastore.put(network);


                }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2025-01-03 05:35:30
  1. 您不必获取并放置实体即可更新它。如果您知道实体的 ID,则只需输入即可。如果存在,则会更新,如果不存在,则会创建。

  2. 使用objectify自动将您的类映射到实体。

  1. You don't have to get and put the entity in order to update it. If you know the ID of the entity you can just put it. If it exists it will be updated, if not it will be created.

  2. Use objectify to automatically map your classes to entities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文