DBRefs(Mongo文档引用)没有急切地获取

时间:2022-09-11 16:52:41

I am using Mongo in its simplest avatar possible (in conjunction with Spring Data).

我在最简单的化身中使用Mongo(与Spring Data一起使用)。

I have two (first class) entities (@Documents) A and B, where A has a reference (@DBRef) of B within it. Everything works fine when creating A and B. However, when reading object A (by Id), the reference B is always null.

我有两个(第一类)实体(@Documents)A和B,其中A有一个B的引用(@DBRef)。创建A和B时一切正常。但是,当读取对象A(通过Id)时,引用B始终为null。

I believe DBRefs are eagerly fetched by default (see http://static.springsource.org/spring-data/data-document/docs/current/reference/html/#mapping-usage-references), but the behavior currently is against that. Any ideas why?

我相信默认情况下会急切地获取DBRef(参见http://static.springsource.org/spring-data/data-document/docs/current/reference/html/#mapping-usage-references),但目前的行为是反对的那。有什么想法吗?

2 个解决方案

#1


2  

You are correct, any DBRefs are eagerly fetched, but they are not eagerly saved (AFAIK). If A has a reference to B, when you save A, Spring Data/MongoDB doesn't automatically save B, you have to.

你是对的,任何DBRef都是急切的获取,但他们并没有急切地保存(AFAIK)。如果A有对B的引用,当你保存A时,Spring Data / MongoDB不会自动保存B,你必须这样做。

// Incorrect, upon retrieval a.getB() == null
A a = new A();
a.setB(new B());
repositoryA.save(a);

// Correct (to the best of my knowledge)
B b = repositoryB.save(new B());
A a = new A();
a.setB(b);
repositoryA.save(a);

#2


1  

Moving over to the Spring Data Mongo M5 build resolved this. So, must be a bug until then.

转移到Spring Data Mongo M5构建解决了这个问题。所以,在那之前一定是个bug。

#1


2  

You are correct, any DBRefs are eagerly fetched, but they are not eagerly saved (AFAIK). If A has a reference to B, when you save A, Spring Data/MongoDB doesn't automatically save B, you have to.

你是对的,任何DBRef都是急切的获取,但他们并没有急切地保存(AFAIK)。如果A有对B的引用,当你保存A时,Spring Data / MongoDB不会自动保存B,你必须这样做。

// Incorrect, upon retrieval a.getB() == null
A a = new A();
a.setB(new B());
repositoryA.save(a);

// Correct (to the best of my knowledge)
B b = repositoryB.save(new B());
A a = new A();
a.setB(b);
repositoryA.save(a);

#2


1  

Moving over to the Spring Data Mongo M5 build resolved this. So, must be a bug until then.

转移到Spring Data Mongo M5构建解决了这个问题。所以,在那之前一定是个bug。