title: MongoDB DBRef list returning null in Spring Boot
body: <p>Im trying to create a database in MongoDB using Spring Boot. I want to create two collections, a DbConnections collection and a Configuration collection. What I want to do is that each connection in DbConnections collection has it own Configurations, something as a SQL references using foreign keys. I have trying to do this using <code>@DBRef</code> annotation like this:</p>
  
  <pre><code>@Document(collection = "connections")
  public class DbConnection {
      @Transient
      public static final String SEQUENCE_NAME = "connection_sequence";
      @Id
      private long id;
      private String username;
      private String password;
      private String connectionUrl;
      private long fkIdUsuario;
      @DBRef(db = "configurations")
      private List&lt;Configuration&gt; configurations;
  
      public DbConnection() {
      }
  
      public DbConnection(String username, String password, String connectionUrl) {
          this.username = username;
          this.password = password;
          this.connectionUrl = connectionUrl;
      }
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public List&lt;Configuration&gt; getConfigurations() {
          return configurations;
      }
  
      public void setConfigurations(List&lt;Configuration&gt; configurations) {
          this.configurations = configurations;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public String getConnectionUrl() {
          return connectionUrl;
      }
  
      public void setConnectionUrl(String connectionUrl) {
          this.connectionUrl = connectionUrl;
      }
  
      public long getFkIdUsuario() {
          return fkIdUsuario;
      }
  
      public void setFkIdUsuario(long fkIdUsuario) {
          this.fkIdUsuario = fkIdUsuario;
      }
  }
  </code></pre>
  
  <p>And the Configuration class:</p>
  
  <pre><code>@Document(collection = "configurations")
  public class Configuration {
      @Transient
      public static final String SEQUENCE_NAME = "config_sequence";
      @Id
      private long id;
      private int fkIdConnection;
      private String name;
      private String query;
      private BasicDBObject values;
      private BasicDBObject result;
  
      public Configuration() {
      }
  
      public Configuration(long id, int fkIdConnection, String name, String query, BasicDBObject values, BasicDBObject result) {
          this.id = id;
          this.fkIdConnection = fkIdConnection;
          this.name = name;
          this.query = query;
          this.values = values;
          this.result = result;
      }
  
      public int getFkIdConnection() {
          return fkIdConnection;
      }
  
      public void setFkIdConnection(int fkIdConnection) {
          this.fkIdConnection = fkIdConnection;
      }
  
      public Map getValues(){
          if(null != values){
              return values.toMap();
          }
          return null;
      }
  
      public void setValues(){
          this.values = new BasicDBObject(values);
      }
  
      public Map getResult(){
          if(null != result){
              return result.toMap();
          }
          return null;
      }
  
      public void setResult(){
          this.result = new BasicDBObject(result);
      }
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getQuery() {
          return query;
      }
  
      public void setQuery(String query) {
          this.query = query;
      }
  }
  </code></pre>
  
  <p>And I save the data like this in the controllers:
  For DbConnectionsController:</p>
  
  <pre><code>@PostMapping(value = "/conexion")
      public DbConnection addConnection(@RequestBody DbConnectionModel dbConnectionModel){
          DbConnection dbConnection = new DbConnection(dbConnectionModel.getUsername(), dbConnectionModel.getPassword(), dbConnectionModel.getConnectionUrl());
          dbConnection.setFkIdUsuario(getIdUsuario());
          return dbConnectionRepository.save(dbConnection);
      }
  </code></pre>
  
  <p>And the Configuration controller save method:</p>
  
  <pre><code>@PostMapping
      public Configuration agregarConfiguracion(@RequestBody Configuration configuration){
          configuration.setId(sequenceGeneratorService.generateSequence(Configuration.SEQUENCE_NAME));
  
          return configurationRepository.save(configuration);
      }
  </code></pre>
  
  <p>Now, when I save a <code>Connection</code>, it doesn't have the field <code>configurations</code> in the mongo object, and when I create a new <code>Configuration</code> it stills doesn't show it in <code>DbConnection</code>.I try to fetch the DbConnections objects with a <code>GET</code> request, but the <code>configurations</code> field of <code>DbConnections</code> appears as null. Why is this happening and how can I solve it?</p>