有人可以在Ektorp中解释我的Cascading和FetchType惰性吗? [英] Can someone explain me Cascading and FetchType lazy in Ektorp?

查看:115
本文介绍了有人可以在Ektorp中解释我的Cascading和FetchType惰性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CouchDB和Ektorp的新手(我从今天开始实际尝试使用它).我找到的最详细的文档可以帮助我入门:

I am new to CouchDB and Ektorp (I ACTUALLY started today to try to work with it). The most detailed documentation I have found to help me getting started is this one:

http://www.ektorp.org/reference_documentation.html#d100e394

我的用例是我想将一个非常复杂的类另存为文档(到目前为止,我已经进行了管理),但是我不想一直加载所有字段(因为其中某些字段可能是大集合)其他较简单的文档).

My use case is that I want to save a very complex class as a document (I have managed that so far), but I do not want to load all the fields all the time (since some of those are potentially big collections of other simpler documents).

这是我所拥有的一个例子(它只是我用来学习使用Ektorp和CouchDB的实验课程

Here is an example of what I have (its just an experimental class I am using to learn to use Ektorp and CouchDB

@JsonSerialize(include = Inclusion.NON_NULL)
public class Player extends CouchDbDocument {

    private int xp = 0;

    @JsonDeserialize(using = CoinPouchDeserializer.class)
    private CoinPouch coins = new CoinPouch(); // subclass of enumMap not
                           // complex
    @DocumentReferences(backReference = "playerId", fetch = FetchType.LAZY, descendingSortOrder = true, orderBy = "itemid")
    private Inventory inventory = new Inventory();// subclass of Map<String,
                          // Item> Items are document
                          // themselves
}

我设法保存它并通过id获得它就好了.但是如何在不加载库存的情况下获取它呢?
我还要感谢与其他资源的任何链接,我应该结帐一下有关开始在Java(或Scala)中使用沙发床或ektorp,欢呼.

I manage to save it and get it by id just fine. But how to I get it without loading it inventory?
I would also appreciate any link to other resources I should checkout about starting to use couchdb or ektorp with java (or scala), cheers.

感谢您提供任何有用的答案.

Thanks for any helpful answer.

推荐答案

像这样的文档引用基本上与SQL DB中的JOIN相似,但是您不能像在SQL中那样在一个请求中执行JOIN.相反,您需要先请求以获取所需的核心文档,然后再请求以获取任何引用的文档.

Document references like these basically act similarly to JOINs in a SQL DB, but you can't do a JOIN in one request as you can with SQL. Instead you need to make first request to get the core document you're looking for, and then a second request to get any referenced documents.

将FetchType设置为eager会告诉Ektorp在阅读第一个文档后立即执行此操作,因此会立即对所有引用的文档进行一系列请求,确保在开始使用前加载所有内容. FetchType lazy不会执行此操作,而是会忽略引用的文档,直到您尝试使用它们为止.

Setting FetchType to eager tells Ektorp to do this as soon as you read the first document, so a series of requests are then made immediately for all referenced documents, ensuring everything is loaded before you start using it. FetchType lazy doesn't do this, and instead ignores referenced documents until you try and use them.

通常,如果不太可能使用引用的文档,则希望延迟加载.如果您始终要使用它们,那么急于加载可能会更好,因为这样做至少可以为您提供一致的前期加载时间,而不是在过程的后期意外地发出请求.听起来,虽然您不想在加载播放器时加载库存,所以可以,将FetchType设置为lazy应该可以解决该问题.

Generally you want lazy loading if it's unlikely you're going to be using the referenced documents. If you're always going to be using them eager loading is probably better, as it does at least give you a consistent up-front loading time, rather than having requests made unpredictably later in the process. By the sound of it you don't want to load the inventory when you load a player though, so yes, setting FetchType to lazy should solve that problem.

与此同时,级联参数可让您配置在此文档上执行操作(更新删除等)时对引用文档的处理方式.默认情况下,引用的文档需要自己显式保存或删除,您不能只保存主文档并期望其他文档也被保存.听起来,这实际上就是您想要的行为(否则,将更改保存到播放器中将被加载,然后保存库存).

Meanwhile the cascade parameter lets you configure what happens to referenced documents if you perform operations (update delete etc) on this document. By default referenced documents need to be explicitly saved or deleted themselves, you can't just save the main document and expect the others to be saved too. By the sound of it that's actually the behaviour you want anyway (as otherwise saving a change to the player will load and then save the inventory, I believe).

另外,根据文档,您还应该注意您的示例实际上是无效的,因为您只能使用DocumentReferences来引用其他文档的Set(实际的Java Set实现),而您是Map.

Also you should note that your example isn't actually valid, according to the documentation, as you can only use DocumentReferences to refer to Sets (actual bonafide Java Set implementations) of other documents, and yours is a Map.

这篇关于有人可以在Ektorp中解释我的Cascading和FetchType惰性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆