title: MongoDB POJO and inner/nested classes
body: <p>I've got a question regarding MongoDB Java driver and POJOs/serialization.
  I'd like to build up my Java classes as they are represented in the MongoDB collection and then use the (new) POJO feature of MongoDB for fetching data. See: <a href="http://mongodb.github.io/mongo-java-driver/3.8/driver-async/getting-started/quick-start-pojo/" rel="nofollow noreferrer">http://mongodb.github.io/mongo-java-driver/3.8/driver-async/getting-started/quick-start-pojo/</a> and <a href="http://mongodb.github.io/mongo-java-driver/3.8/bson/pojos/" rel="nofollow noreferrer">http://mongodb.github.io/mongo-java-driver/3.8/bson/pojos/</a></p>
  
  <p>Right now this only works if I have two different classes, like</p>
  
  <p>User.class
  </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.UUID;
  import org.bson.codecs.pojo.annotations.BsonId;
  import org.bson.codecs.pojo.annotations.BsonProperty;
  
  public class User {
    @BsonId
    @BsonProperty("_id")
    UUID id;
    private List&lt;UserSession&gt; sessions = new ArrayList&lt;&gt;();
  }
  </code></pre>
  
  <p>UserSession.class
  </p>
  
  <pre><code>import java.time.Instant;
  
  public class UserSession {
    Instant start;
    Instant end;
  }
  </code></pre>
  
  <p>But as my collection looks more like the following…</p>
  
  <pre class="lang-js prettyprint-override"><code>{
    _id: XYZ,
    sessions: {
      {start: XYZ, end: XYZ},
      {start: XYZ, end: XYZ},
      {start: XYZ, end: XYZ}
    }
  }
  </code></pre>
  
  <p>… I'd like to have a class that looks like this:</p>
  
  <p>User.class
  </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  import java.util.UUID;
  import java.time.Instant;
  import org.bson.codecs.pojo.annotations.BsonId;
  import org.bson.codecs.pojo.annotations.BsonProperty;
  
  public class User {
    @BsonId
    @BsonProperty("_id")
    UUID id;
    private List&lt;Session&gt; sessions = new ArrayList&lt;&gt;();
  
    public class Session {
      Instant start;
      Instant end;
    }
  }
  </code></pre>
  
  <p>This makes sense as every unique sessions belongs directly to a user and with being a nested class I'd be able to access fields of the parent User object from within the Session object.</p>
  
  <p>The problem is that the Java driver now complains about not having an empty/no arguments constructor for my Session class ("By default all POJOs must include a public or protected, empty, no arguments, constructor.").</p>
  
  <p>My CodecProvider is like following:</p>
  
  <pre class="lang-java prettyprint-override"><code>CodecProvider codecProvider = PojoCodecProvider.builder()
      .register(User.class, User.Session.class);
  </code></pre>
  
  <p>Anyone here having an idea how to solve this issue?</p>
  
  <p>Really appreciate your help!</p>
  
  <p>Thanks a lot!</p>
  
  <p><em>Side note: The code snippets above are just short examples how my code kinda looks like. They are not the full code I'm using so there might be syntax errors in it.</em></p>