GWT RequestFactory:如何从 stableId() 获取持久 id?

发布于 2024-10-18 10:13:24 字数 1258 浏览 4 评论 0原文

我在实体中使用Long id,不仅将它们存储在数据存储中,而且还引用其他实体。现在,我使用 RequestFactory 在客户端上 create() 对象并保留它们,但我需要一种方法来找出服务器生成的 id。

这是我发现的一种需要两次旅行的方法:

final OrganizationProxy proxy = context.create(OrganizationProxy.class);
context.persist().using(proxy).fire(new Receiver<Void>(){

    public void onSuccess(Void response)
    {
        requestFactory.find(proxy.stableId()).fire(new Receiver<OrganizationProxy>()
        {
            public void onSuccess(OrganizationProxy response)
            {
                //hey, now response has the server-generated id in it, along with any other values the server populated
            }
        });
    }
});

但似乎必须有一种方法无需第二次旅行即可获取持久ID。看起来 requestFactory.find() 首先需要持久 id 才能工作。

如何在不向服务器发出第二次请求的情况下获取持久 ID?

=======更新=======

我终于想到了(在tbroyer 告诉我 ;)) 我可以返回Long id。这不会从 EntityProxyId 中检索持久 ID,但它确实可以在单个请求中获取新对象的持久 ID。

我将保留这个问题 - 我仍然对从 EntityProxyId 中获取持久 id 感兴趣。

I use Long ids in my entities, not only to store them in the datastore, but to reference other entities. Now, I'm using RequestFactory to create() objects on the client and persist them, but I need a way to figure out what id the server has generated.

Here's one way I've figured out that requires two trips:

final OrganizationProxy proxy = context.create(OrganizationProxy.class);
context.persist().using(proxy).fire(new Receiver<Void>(){

    public void onSuccess(Void response)
    {
        requestFactory.find(proxy.stableId()).fire(new Receiver<OrganizationProxy>()
        {
            public void onSuccess(OrganizationProxy response)
            {
                //hey, now response has the server-generated id in it, along with any other values the server populated
            }
        });
    }
});

But it seems like there must be a way to get the persistent id without the second trip. It seems like requestFactory.find() would need the persistent id to work at all in the first place.

How can I get at the persistent id without a second request to the server?

=======Update=======

It finally occurred to me (after tbroyer told me ;)) that I could return the Long id from the persist() method in the RequestContext. This doesn't retrieve the persistent id from the EntityProxyId, but it does get me the persistent id of a new object in a single request.

I'll leave this question open - I am still interested in getting the persistent id out of an EntityProxyId.

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

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

发布评论

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

评论(3

寒尘 2024-10-25 10:13:24

您可以通过调用 RequestFactory.getHistoryToken()。可以通过调用 RequestFactory.getProxyId()

用 RequestFactory 的话说,新创建的实体的 id 是“短暂的”。临时 ID 仅在创建用于创建 EntityProxyRequestContextRequestFactory 实例内有效。当 RequestContext 被触发并且所有方法调用都已由服务器处理时,SimpleRequestProcessor 将检查有效负载的临时对象,如果它们已被持久化,则生成的有效负载将更新客户端的状态以及从 getId() 方法返回的值。

EntityProxyId 从短暂状态切换到持久状态时,它的对象标识和相等性不会改变,但其关联的历史标记将会改变:

OrganizationProxy proxy = context.create(OrganizationProxy.class);
final EntityProxyId<?> id = proxy.stable();
String ephemeralString = factory.getHistoryToken(id);
context.persist().using(proxy).fire(new Receiver<Void>() {
  public void onSuccess(Void response) {
    String persistedString = factory.getHistoryToken(id);
    assert !ephemeralString.equals(persistedString);
    assert factory.getProxyId(ephemeral) == factory.getProxyId(persistedString);

    MyRequestFactory otherFactory = GWT.create(MyRequestFactory.class);
    assert factory.getProxyId(ephemeral) != otherFactory.getProxyId(ephemeral);
    // Throws IllegalArgumentException
    otherFactory.find(otherFactory.getProxyId(ephemeral));
  }
});

在上面的演示中,一旦你有了 persistedString< /code>,您可以将值存储在 cookie 或其他客户端持久性机制中,并在稍后重新创建 id,以便在调用 RequestFactory.find() 时使用。第二个和第三个断言展示了临时 id 的“范围”。临时形式和持久形式可以与创建对象的 RequestFactory 互换使用。如果将临时 id 与新创建的 RequestFactory 实例一起使用(如果将临时 id 添加为 History 令牌作为书签,则会发生这种情况),您将获得EntityProxyId,但它不能真正用于任何有用的目的。

You can get a String representation of an EntityProxyId by calling RequestFactory.getHistoryToken(). This can be converted back to an EntityProxyId by calling RequestFactory.getProxyId().

In RequestFactory parlance, the id of a newly-created entity is "ephemeral." An ephemeral id is only valid within the instance of the RequestFactory that created the RequestContext used to create the EntityProxy. When the RequestContext is fired and all method invocations have been processed by the server, the SimpleRequestProcessor will check the payload's ephemeral objects and if they have been persisted, the resulting payload will update the client's state with the value returned from the getId() method.

The object identity and equality of an EntityProxyId won`t change when it switches from the ephemeral to persistent state, but its associated history token will change:

OrganizationProxy proxy = context.create(OrganizationProxy.class);
final EntityProxyId<?> id = proxy.stable();
String ephemeralString = factory.getHistoryToken(id);
context.persist().using(proxy).fire(new Receiver<Void>() {
  public void onSuccess(Void response) {
    String persistedString = factory.getHistoryToken(id);
    assert !ephemeralString.equals(persistedString);
    assert factory.getProxyId(ephemeral) == factory.getProxyId(persistedString);

    MyRequestFactory otherFactory = GWT.create(MyRequestFactory.class);
    assert factory.getProxyId(ephemeral) != otherFactory.getProxyId(ephemeral);
    // Throws IllegalArgumentException
    otherFactory.find(otherFactory.getProxyId(ephemeral));
  }
});

In the above demo, once you have persistedString, you can stash the value in a cookie or some other client-side persistence mechanism and re-create the id later to be used with a call to RequestFactory.find(). The second and third assertions demonstrate the "scope" of an ephemeral id. The ephemeral and persisted forms can be used interchangeably with the RequestFactory that created the object. If an ephemeral id is used with a newly-created instance of a RequestFactory (which would happen if an ephemeral id were bookmarked as a History token), you'll get an EntityProxyId, but it can't really be used for any useful purpose.

早乙女 2024-10-25 10:13:24

实现 EntityProxyId 的类是 SimpleEntityProxyId。该类有一个方法 getServerId(),它将返回 id。因此,通过检查 instanceof 您就可以调用该方法。 (实际上 RequestFactory.getHistoryToken() 甚至不检查,只是简单地转换为此类)。

现在有坏消息:它已被编码,并且是 SimpleEntityProxyId 的基类,即 SimpleProxyId 并包含方法 getServerId(),具体指出:

编码的地址对于客户端来说是完全不透明的。它可能是一个 base64 编码的字符串,但也可能是 pi 的数字。除了将此字段的内容发送回服务器之外的任何代码都是错误的。

(其中字段 encodedAddress 包含服务器 ID。)

The class implementing an EntityProxyId is SimpleEntityProxyId. This class has a method getServerId(), which will return the id. So by checking with instanceof you can then call the method. (Actually RequestFactory.getHistoryToken() doesn't even check, but simply casts to this class).

Now the bad news: it's encoded and the base class for SimpleEntityProxyId, which is SimpleProxyId and contains the method getServerId(), specifically states:

The encodedAddress is totally opaque to the client. It's probably a base64-encoded string, but it could be digits of pi. Any code that does anything other than send the contents of this field back to the server is wrong.

(Where the field encodedAddress contains the server id.)

-黛色若梦 2024-10-25 10:13:24

我刚刚在 Proxy 接口中声明了 getId() 方法,它似乎有效。这种做法有什么问题吗?

I just declared the getId() method in my Proxy interface and it seems to work. Is there any problem with this approach?

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