MongoDB POJO and inner/nested classes

616 views
Skip to first unread message

Sebastian Binder

unread,
Sep 18, 2018, 3:41:52 AM9/18/18
to mongodb-user
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: http://mongodb.github.io/mongo-java-driver/3.8/driver-async/getting-started/quick-start-pojo/ and http://mongodb.github.io/mongo-java-driver/3.8/bson/pojos/

Right now this only works if I have two different classes, like

User.class
   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<UserSession> sessions = new ArrayList<>();
   }

UserSession.class
    import java.time.Instant;

    public class UserSession {
     Instant start;
     Instant end;
   }

But as my collection looks more like the following…
    {
     _id: XYZ,
     sessions: {
       {start: XYZ, end: XYZ},
       {start: XYZ, end: XYZ},
       {start: XYZ, end: XYZ}
     }
   }


… I'd like to have a class that looks like this:

User.class
   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<Session> sessions = new ArrayList<>();
   
     public class Session {
       Instant start;
       Instant end;
     }
   }

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.

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.").

My CodecProvider is like following:
    CodecProvider codecProvider = PojoCodecProvider.builder()
       .register(User.class, User.Session.class);


Anyone here having an idea how to solve this issue?

Really appreciate your help!

Thanks a lot!

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. It might be helpful to have a look on my non-resolved thread on Stackoverflow as there has been a few comments for clarification: https://stackoverflow.com/questions/52309025/mongodb-pojo-and-inner-nested-classes

Wan Bachtiar

unread,
Sep 27, 2018, 3:00:25 AM9/27/18
to mongodb-user

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.

Hi Sebastian,

Just to clarify, you’re asking about nesting the Session class inside of User ?
Is there a reason not to make the classes both public ? 

public class User {
    @BsonId
    @BsonProperty("_id")
    UUID id;
    private List<Session> sessions = new ArrayList<>();
}
public class Session {
    Instant start;
    Instant end;
}

Regards,
Wan

Sebastian Binder

unread,
Sep 28, 2018, 2:55:45 AM9/28/18
to mongodb-user
Hi Wan,

yes, you are right. I try to nest the Session class inside of User . If i have two separate classes I cannot do things like  this.User.id 
from within my Session for getting the UUID of the parent User . Having a separate class with a field id in my Session class does not really represent my database layout and that's what POJOs in my opinion are supposed to do. They should represent the model of the database. Furthermore this cannot be handled by standard MongoDB POJO implementation.

Regards,
Sebastian
Reply all
Reply to author
Forward
0 new messages