Java Google App Engine数据存储区put / get

时间:2013-10-16 14:40:08

标签: java google-app-engine

The Put:

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    try{
        Entity e =  new Entity("Winners", "NFL" ); 
        String winner_game = new String("winner_" + input_game );
        e.setProperty(winner_game, input_win_value );
        datastore.put(e);

...

获取:

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    try{
        Entity e = datastore.get(KeyFactory.createKey("Winners",  "NFL" )); 
        String winner_game = new String("winner_" + i );
        if( e.getProperty(winner_game) != null )
        {
            Integer property = new Integer(e.getProperty( winner_game ).toString());
            return property;
        }
        else{
            return 0;
        }

...

e.getProperty返回null .....任何想法?

1 个答案:

答案 0 :(得分:1)

不要创建新的String / Integer对象。如果您需要在属性名称中附加内容,请直接执行。

Entity e = new Entity("Winners", "NFL" ); 
String winner_game = "winner_" + input_game; // Where input_game is "X"
e.setProperty(winner_game, input_win_value); // int input_win_value = 10;
datastore.put(e);

检索相同:

Entity e = datastore.get(KeyFactory.createKey("Winners",  "NFL" )); 
String winner_game = "winner_" + i; // where i is also "X"
if( e.getProperty(winner_game) != null ) {
   // Casting is ugly but does the trick
   Integer property = (Integer) e.getProperty(winner_game);
   return property;
} else{
   return 0;
}