0

I am trying to have table per class configuration with Morphia + MongoDb

@Entity("user")
public class User{
     @Id
     private ObjectId id;
     private String username;

     public void setId(ObjectId id){
           this.id = id;
     }
     public ObjectId getId(){
           return this.id;
     }
     public void setUsername(String username){
           this.username = username;
     }
     public String getUsername(){
          return this.username;
      }
}

And Assistant

@Entity("assistant")
public class Assistant extends User{
       private String fullname;
       public String getFullname(){
              return this.fullname;
       }
       public void setFullname(String fullname){
              this.fullname = fullname;
       }
}

I Want to have a user document and assistant document with same ObjectId. How Do this?

Alireza Khajavi
  • 146
  • 1
  • 10

1 Answers1

1

MongoDB is not a relational database, you will need to change your mental & database model.

If somebody is an assistant, all the data will go to the assistant collection. There are no joins (except for aggregations, but you don't want to do this for a simple user lookup), so all the information should be in one entity.

You could even keep the assistants in the user collection. Or speaking in more general terms: Keep all subclasses in the entity of its parent class. Morphia will automatically store the Java class for every entity, so you can simply filter on that. And since there is no strict schema, there are also no null values since the JSON only stores the available attributes.

xeraa
  • 10,456
  • 3
  • 33
  • 66