GWT + GAE 数据存储键和文本 Java 错误

发布于 2024-12-02 00:42:54 字数 1911 浏览 1 评论 0原文

我想创建一个应用程序来保存和检索记录到 GAE 服务器。我按照教程“部署到 Google App Engine”http://code 进行操作。 google.com/webtoolkit/doc/latest/tutorial/appengine.html 开始使用。

我现在有 StockWatcher 应用程序正在运行,但在我的应用程序中我需要存储一个可能很大的字符串(> 10KB)。我读到我无法使用 Java String 类型来存储大字符串,需要使用 Text 数据类型。

我认为“文本”的意思是:com.google.appengine.api.datastore.Text,但最好确认这是正确的。 ???

无论如何,我无法让 Text 工作。经过一番研究,发现 Key 和 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
  {
    ...

我看到一些发布的解决方案建议使用非标准的第 3 方库,例如 http://www.resmarksystems.com/code/。我无法安装这个,但即使可以,我也更喜欢另一种解决方案。存储文本一定是一项常见的任务,我更愿意使用标准解决方案来解决这个问题。

我可以更改代码以在多个 get 方法中返回每个字段,而不是单个返回 MyDataRecord 实例。然而,即使这有效,随着时间的推移,工作量也会显着增加,维护起来也更加困难。然而,如果这是通常所期望的,那么我就会这么做。

我想使用 GWT 和 GAE 认为的最佳实践来解决这个问题。一个简单的示例或教程会有很大帮助,但我找不到。

是否有示例程序/教程显示 GWT 认为存储和检索大型字符串的最佳实践?

我是 GWT 和 GAE(以及 Web 开发)的新手,请在任何回复中考虑这一点, 谢谢。

请不要恶作剧

I would like to create an application that saves and retrieves records to the GAE server. I followed the tutorial "Deploying to Google App Engine" http://code.google.com/webtoolkit/doc/latest/tutorial/appengine.html to get started.

I have the StockWatcher application working now, but in my application I need to store a String that can be large (>10KB). I read that I can't use a Java String type to store large strings and need to use the Text data type instead.

I think by Text, they mean: com.google.appengine.api.datastore.Text, but it would be nice to confirm this is correct. ???

Regardless, I can't get Text to work. After some research it appears both the types Key and Text can only be used in the server code and not the client code. It seems that this is because the source code is not available for these classes and GWT needs the source to create the JavaScript code on the client's computer. At least that my current working hypothesis as to why I'm getting the following errors:

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

I use the following fields in a class in a shared folder.

shared/MyDataRecord

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

@Persistent
private Text description;

MyDataRecord class in a shared folder because I wanted to use to send back all the fields in one get method return rather than multiple individual field get methods. Here's how I use MyDataRecord class in my server/DataServiceImpl.java class

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

I've seen some posted solutions suggest using non-standard, 3rd party libraries, like http://www.resmarksystems.com/code/. I couldn't get this one installed, but even if I could, I'd prefer a different solution. Storing Text must be such a common task that I'd prefer to solve this using what is considered a standard solution.

I could change my code to return each field in multiple get methods instead of an single return of a MyDataRecord instance. However, even if that works, that would be significantly more work and more difficult to maintain over time. However, if this is what is normally expected, then that's what I'll do.

I'd like to solve this using what is considered best practices by GWT and GAE. A simple example or tutorial would go a long way, but I can't find one.

Are there example programs/tutorials that show what GWT considers best practices for storing and retrieving large strings?

I am a newbie with both GWT and GAE (as well as web development), please consider this in any responses, thanks.

No Snark Please

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

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

发布评论

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

评论(2

墨洒年华 2024-12-09 00:42:54

可序列化的 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)从数据库加载父级
2)识别该父级的子级,并加载它
3) 转换child.description->parent.description
4) 现在您有了一个完全构造的可序列化的父 POJO。将其发送到 UI

从 UI 返回到 GAE 时只需执行相反的过程即可。

The serializable POJO. Note the NotPersistent annotation for description

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;

}

The second POJO. Notice the package

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.
}

Notice the parent ID mapped in the child. While retrieving you will need to identify which child belongs to which parent.
In pseudo code
1) Load parent from DB
2) Identify child for this parent, and load it
3) Convert child.description->parent.description
4) Now you have a fully constructed parent POJO which is serializable. Send it to the UI

Just reverse the procedure on the way back from UI to GAE.

疯到世界奔溃 2024-12-09 00:42:54

1) 在可序列化 POJO 私有字符串描述中定义一个 NotPersistent 字段
2) 在服务器端定义一个新的POJO,该POJO将具有私有文本描述
3) 当您持久/加载原始 POJO 时,检索新的 POJO 并从文本描述中填充字符串描述

1) Define a NotPersistent field in your serializable POJO private String description
2) Define a new POJO on the server side which will have private Text description
3) When you persist/load the original POJO, retrieve the new POJO and populate the String description from the Text description

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