0
我正在使用Google App Engine并将Google的数据存储接口用于数据库。 我的问题是这样的,我有以下代码:我有一个网络对象,如果它存在数据库上,我想更新它,或者如果它是第一次创建。 。为此,我必须捕捉一个异常并重复相同的代码两次 - 它看起来很丑陋,多余,让我觉得我做错了什么。 第二件让我感到奇怪的事情是,我没有办法想到将对象复制到实体中,反之亦然。我希望自己实现这个吗?对每个属性使用setProperty或getProperty是非常不容易的......我只是想知道为什么没有objectToEntity方法或类似的东西。Google App Engine数据存储困难
这是我的代码目前的样子......
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);
}
谢谢彼得! – 2012-01-16 05:55:44