DBRefs (Mongo Document references) not fetched eagerly

别等时光非礼了梦想. 提交于 2019-11-30 17:56:25

问题


I am using Mongo in its simplest avatar possible (in conjunction with 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.

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?


回答1:


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.

// 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:


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



来源:https://stackoverflow.com/questions/8068807/dbrefs-mongo-document-references-not-fetched-eagerly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!