Search code examples
javamongodborminner-classesmorphia

ORM in Morphia for a Nested Class


My Json document in the Morphia DB looks like this -

{
    "_id" : ObjectId("58fcdf7e"),
    "status" : "ACTIVE",
    "user" : {
        "id" : NumberLong(228),
        "email" : "[email protected]"
    } 
}

I have created a Java class for this collection which looks like this -

@Entity("member_offer")
public class MemberOffer {
  @Id
  private ObjectId objectId;

  @Property("status")
  private String status;

  @Embedded("user")
  private UserDetail user;

  @Embedded
  class UserDetail {
    @Property("id")
    public long memberId;

    @Property("email")
    public String email;

    UserDetail() {

    }
  }

  public ObjectId getObjectId() {
    return objectId;
  }

  public void setObjectId(ObjectId objectId) {
    this.objectId = objectId;
  }

  public String getStatus() {
    return status;
  }

  public void setStatus(String status) {
    this.status = status;
  }

  public UserDetail getUser() {
    return user;
  }

  public void setUser(UserDetail user) {
    this.user = user;
  }
}

Now when I am trying to fetch the data I am getting this exception -

java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException: No usable constructor for vo.MemberSubscription$UserDetail

Caused by: org.mongodb.morphia.mapping.MappingException: No usable constructor for vo.MemberSubscription$UserDetail

Caused by: org.mongodb.morphia.mapping.MappingException: No usable constructor for vo.MemberSubscription$UserDetail

Caused by: java.lang.NoSuchMethodException: vo.MemberSubscription$UserDetail.()

Any idea how I can resolve this issue? I want UserDetail to be nested class only, I know if I create it as an independent class this error can be resolved. But my question here is can something like this (having nested class) can be achieved in Morphia?

Also if there is some fundamental flaw in my design please educate me about it.


Solution

  • You should try to use public modifier for the constructor, also make UserDetail (inner class) is static.