2014-05-15 53 views
3

我想從我的數據存儲使用objectify刪除一個實體,但似乎並沒有被刪除,即使關閉實例並重新啓動它。這是實體看起來像在數據存儲中的內容(既當它是生產服務器&開發服務器上):Objectify刪除似乎沒有工作

enter image description here

這是我使用的嘗試,並刪除它的代碼:

@ApiMethod(name = "deleteDataVersion") 
public Result deleteDataVersion(@Named("id") String id) { 

    // Where id is the id of the entity in the datastore. 

    if (id != null && !id.equals("")) { 
     ofy().delete().type(DataVersion.class).id(id).now(); 
     return new Result(Result.STATUS_SUCCESS); 
    } else 
     return new Result(Result.STATUS_FAILED); 
} 

我也試過這樣的代碼:

@ApiMethod(name = "deleteDataVersion") 
public Result deleteDataVersion(@Named("id") String id) { 

    if (id != null && !id.equals("")) { 

     // DataVersion doesn't have a parent. 
     Key<DataVersion> key = Key.create(null, DataVersion.class, id); 

     ofy().delete().key(key).now(); 
     return new Result(Result.STATUS_SUCCESS); 
    } else 
     return new Result(Result.STATUS_FAILED); 
} 

但實體永遠不會被刪除。這是我的實體代碼:

@Entity 
public class DataVersion { 

    @Id 
    private Long id; 
    String folderName; 
    @Index 
    String effective; 

    public DataVersion() { 
    } 

    public DataVersion(String folderName, String effective) { 
     this.folderName= folderName; 
     this.effective = effective; 
    } 

    // Getters & setters.. 
} 

我似乎無法找到問題:(任何幫助將不勝感激我敢肯定這件事情未成年我俯瞰(相當新的客體/ AppEngine上)。

回答

5

你必須在參數中的端點ID是一個字符串,並嘗試刪除的對象DataVersion其中ID是一個漫長的。

ofy().delete().type(DataVersion.class).id(Long.valueOf(id)).now(); 

會更好地工作!

+0

id可以是String或Long,但「請注意,數字ID和字符串ID不等效; 123和」123「是不同的ID。 – SpyZip

1

先拿鑰匙。
Key<DataVersion> key = Key.create(null, DataVersion.class, id);

然後使用密鑰從數據庫中獲取實體。
DataVersion dataVersion = ofy().load().key(key).now();

然後使用objectify刪除實體。
ofy().delete().entity(dataVersion).now();