GWT RequestFactory:如何从 stableId() 获取持久 id?
我在实体中使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过调用
RequestFactory.getHistoryToken()
。可以通过调用RequestFactory.getProxyId()
。用 RequestFactory 的话说,新创建的实体的 id 是“短暂的”。临时 ID 仅在创建用于创建
EntityProxy
的RequestContext
的RequestFactory
实例内有效。当RequestContext
被触发并且所有方法调用都已由服务器处理时,SimpleRequestProcessor
将检查有效负载的临时对象,如果它们已被持久化,则生成的有效负载将更新客户端的状态以及从getId()
方法返回的值。当
EntityProxyId
从短暂状态切换到持久状态时,它的对象标识和相等性不会改变,但其关联的历史标记将会改变:在上面的演示中,一旦你有了
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 anEntityProxyId
by callingRequestFactory.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 theRequestContext
used to create theEntityProxy
. When theRequestContext
is fired and all method invocations have been processed by the server, theSimpleRequestProcessor
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 thegetId()
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: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 toRequestFactory.find()
. The second and third assertions demonstrate the "scope" of an ephemeral id. The ephemeral and persisted forms can be used interchangeably with theRequestFactory
that created the object. If an ephemeral id is used with a newly-created instance of aRequestFactory
(which would happen if an ephemeral id were bookmarked as aHistory
token), you'll get anEntityProxyId
, but it can't really be used for any useful purpose.实现
EntityProxyId
的类是SimpleEntityProxyId
。该类有一个方法getServerId()
,它将返回 id。因此,通过检查instanceof
您就可以调用该方法。 (实际上RequestFactory.getHistoryToken()
甚至不检查,只是简单地转换为此类)。现在有坏消息:它已被编码,并且是
SimpleEntityProxyId
的基类,即SimpleProxyId
并包含方法getServerId()
,具体指出:(其中字段
encodedAddress
包含服务器 ID。)The class implementing an
EntityProxyId
isSimpleEntityProxyId
. This class has a methodgetServerId()
, which will return the id. So by checking withinstanceof
you can then call the method. (ActuallyRequestFactory.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 isSimpleProxyId
and contains the methodgetServerId()
, specifically states:(Where the field
encodedAddress
contains the server id.)我刚刚在 Proxy 接口中声明了 getId() 方法,它似乎有效。这种做法有什么问题吗?
I just declared the getId() method in my Proxy interface and it seems to work. Is there any problem with this approach?