DBRefs (links to Mongo Document) are not looking

I use Mongo in its simplest avatar (in combination with Spring Data).

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

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

+4
source share
2 answers

Going to Spring Mongo M5 data compilation resolved this. So there must be a mistake so far.

+1
source

You are right, any DBRefs look forward to, but they are not look forward to (AFAIK). If A has a reference to B, when saving A, Spring Data / MongoDB does not automatically save B, you should.

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

Source: https://habr.com/ru/post/1380423/


All Articles