Spring Data-MongoDB:无法检索属于另一个数据库的DBRef文档 [英] Spring Data - MongoDB: Unable to retrieve DBRef document that belongs to another database

查看:166
本文介绍了Spring Data-MongoDB:无法检索属于另一个数据库的DBRef文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体市场(市场存储在marketDb数据库中的集合中),该实体市场引用了产品(存储在productDb数据库中的集合产品中).

I have a Entity Market(market is stored in a collection within marketDb database) that references Product (stored in a collection product in productDb database ).

/*Entity */
class Market {

@DBRef (db = "productDb")
private Product product;

}

/**市场在集合中存储为:*/

/**Market is stored in the collection as: */

{
  "_id": "4f0bc0e6-b6a8-11e6-a04d-080027e9004f",
  "_class": "com.package.Market",
  "createdById": "123",
  "editedById": "123",
  "name": "Market01", 
  "clientId": NumberLong("1"),
  "version": NumberLong("1"),
  "product": DBRef("product", "456", "productDb", { "$db": "productDb" })
}

当我尝试通过市场ID读取市场时,我能够获取市场,但产品返回的值为空.

When I try to read the Market by its Id, I am able to fetch the Market but Product returns as null.

但是: 1.如果产品存储在与Market属于同一数据库的集合中,则将Product作为在Market上读取的一部分返回. 2.如果在上面的代码中,如果我将产品引用指定为: @DBRef(db ="productDb",lazy = true) 私人产品产品;

However: 1. If the product is stored in a collection that belongs to the same database as Market, then Product is returned as part of read on Market. 2. If in the above code, if I specify the Product reference as: @DBRef (db = "productDb", lazy = true) private Product product;

然后我获得对DBRef的引用,其中填充了Product实体的ID.

then I get a reference to the DBRef with the id of the Product entity populated.

任何人都可以提出建议,为什么当属于电子市场的产品实体属于不同的数据库时,为什么不将它们作为电子市场的读取操作的一部分加载?当两者位于不同的数据库中时,是否需要添加任何转换器/自定义查询来加载Market的参考产品,还是需要为MarketRepository添加其他配置以从productDb数据库读取产品数据?

Can anyone please suggest why the product entity that belongs to the Market is not being loaded as part of read operation on Market when they belong to different databases? Do I need to add any converter / Custom query to load the referenced product for the Market when the two are in different databases or some add additional configuration for MarketRepository to read the product data from productDb database?

谢谢.

我使用的Mongo-Java驱动程序是3.2.2.

The Mongo-Java Driver that I use is 3.2.2.

推荐答案

嘿,您是否尝试过使用fetch="select"fetch="join". lazy ="true",这意味着它将在单独的查询中检索关联对象,但不会在加载对象时检索. fetch ="join",它将始终触发单个查询以从数据库中获取关联对象.

Hey did you tried with fetch="select" or fetch="join". lazy ="true", it means that it will retrieve the association objects in a separate query, but not at the time of loading the object. fetch="join" it will always fire a single query to get the association objects from the database.

这篇关于Spring Data-MongoDB:无法检索属于另一个数据库的DBRef文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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