开发者

How to prevent redundant persistence calls from GWT RequestFactory context?

In a project of mine I have a GWT EntityProxy simplified as follows:

@ProxyFor(value = Item.class, locator = ItemService.class)
public interface ItemProxy extends EntityProxy
{
    String getName();

    // other getters and setters here
}

with a corresponding entity implementation that is a simple JPA annotated entity bean.

I also have a request context:

@Service(value = ItemService.class, locator = InjectingServiceLocator.class)
public interface ItemRequestContext extends RequestContext
{
    Request<List<ItemProxy>> findItems();
}

And the corresponding service and locator implementations:

public class ItemService extends Locator<Item, Long>
{
    @Override
    public Item find(Class<? extends Item> clazz, Long id)
    {
        return getItemFromJpa(id);
    }

    public List<Item> findItems()
    {
        return getAllItemsFromJpa();
    }

    // Remaining Locator and JPA methods skipped
}

When I invoke the findItems method in the GWT request context from the RPC perspective everything seems to work as expected and I get the items list in the callback method to work with in the client.

But from the persistence perspective the implementation does not work as expected: On the server side the method findItems is called as expected fetches my items from the persistence and returns them. Then for every single item the method find is called with the item's id and of course retrieves the items again fro开发者_如何学编程m persistence one after each other.

What causes GWT request factory context to make these useless invocations and how can I prevent it from doing so?


Before returning to the browser, RequestFactory will check each and every domain object it saw (either from the request, or in the service methods' return value) to see whether it still exists or not, and thus determine whether it should tell the client that the object has been deleted (generate an EntityProxyChange event with WriteOperation.DELETE).

This check is done by calling the locator's isLive method, whose default implementation calls find with the object's ID and checks whether the return value is null.

In other words, you can simply override isLive in your locator to provide your own logic, and possibly bypass the call to the persistence layer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜