簡體   English   中英

GWT + GAE數據存儲鍵和文本Java錯誤

[英]GWT + GAE datastore Key and Text Java Error

我想創建一個將記錄保存並檢索到GAE服務器的應用程序。 我按照http://code.google.com/webtoolkit/doc/latest/tutorial/appengine.html教程“部署到Google App Engine”開始。

我現在正在使用StockWatcher應用程序,但是在我的應用程序中,我需要存儲一個可能很大的字符串(> 10KB)。 我讀到我不能使用Java String類型存儲大字符串,而需要使用Text數據類型。

我認為“文字”的意思是:com.google.appengine.api.datastore.Text,但最好確認一下它是正確的。 ???

無論如何,我無法使Text正常工作。 經過一番研究,發現“鍵”和“文本”這兩種類型只能在服務器代碼中使用,而不能在客戶端代碼中使用。 看來這是因為源代碼不適用於這些類,並且GWT需要源代碼才能在客戶端計算機上創建JavaScript代碼。 至少關於我為什么會出現以下錯誤的當前工作假設是:

21:52:52.823 [ERROR] [myapp] Line 15: The import com.google.appengine.api.datastore cannot be resolved
21:52:52.951 [ERROR] [myapp] Line 103: Key cannot be resolved to a type
21:52:53.011 [ERROR] [myapp] Line 106: Text cannot be resolved to a type

我在共享文件夾的類中使用以下字段。

共享/ MyDataRecord

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key id;

@Persistent
private Text description;

共享文件夾中的MyDataRecord類,因為我想用一個get方法返回而不是多個單獨的字段get方法來發送所有字段。 這是我在server / DataServiceImpl.java類中使用MyDataRecord類的方法

public class DataServiceImpl extends RemoteServiceServlet implements DataService
{
...
  @Override
  public MyDataRecord getDataRecord() throws NotLoggedInException
  {
    ...

我已經看到一些發布的解決方案建議使用非標准的第三方庫,例如http://www.resmarksystems.com/code/ 我無法安裝該解決方案,但是即使可以,我還是希望使用其他解決方案。 存儲文本必須是一項常見的任務,我希望使用標准解決方案來解決。

我可以更改代碼以在多個get方法中返回每個字段,而不是單次返回MyDataRecord實例。 但是,即使可行,隨着時間的推移,這將顯着增加工作量,並且更難以維護。 但是,如果這是通常所期望的,那就是我要做的。

我想使用GWT和GAE認為的最佳做法來解決此問題。 一個簡單的示例或教程會走很長一段路,但我找不到。

是否有示例程序/教程顯示GWT認為存儲和檢索大字符串的最佳做法是什么?

我是GWT和GAE(以及Web開發)的新手,請在任何答復中考慮這一點,謝謝。

請不要吃蛇

可序列化的POJO。 請注意NotPersistent批注以進行描述

package com.my.project.shared;

@PersistenceCapable(identityType=IdentityType.APPLICATION,detachable="true")
public class MyParent implements Serializable {

    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    private Long id;
    @NotPersistent //Note the NotPersistent annotation. GAE won't persist this value in big table
    private String description;

}

第二個POJO。 注意包裝

package com.my.project.server;

@PersistenceCapable(identityType=IdentityType.APPLICATION,detachable="true")
public class MyChild implements Serializable{//Not really required to implement Serializable

    @PrimaryKey
    @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
    private Long id;
    @Persistent
    private Long parentID;//Reference to the MyParent
    @Persistent
    private Text description;//The actual value of the description variable.
}

請注意,子代中映射的父代ID。 檢索時,您將需要確定哪個孩子屬於哪個父母。 在偽代碼中1)從DB加載父對象2)標識該父對象的子對象,並加載它3)轉換child.description-> parent.description 4)現在,您已經具有一個完全可構造的父POJO。 發送到用戶界面

只需從UI到GAE的反向過程即可。

1)在可序列化的POJO私有字符串描述中定義一個NotPersistent字段2) 在服務器端定義一個具有私有文本描述的新POJO 3)當持久化/加載原始POJO時,檢索新的POJO並從中填充字符串描述文字說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM