user2303962
user2303962

Reputation: 85

Spring and mongoDB nested documents

I have two objects : User and Permission object, each User have list of Permission and Permission have List of Permission too, means its recursive. In my mongoDB i have only Users collection. i have user object which have two levels of permission, and when im trying to get the User object from the mongoDB , i get only the first level and the list of subpermission returns as null.

am i missing some notation? thx!

**The Result: from the mongo

{"sessionId":"admin","status":"OK","userName":"admin","permissions":[{"name":"MPC","link":"#/Release","subPermissions":null},{"name":"MPC PE","link":"#","subPermissions":null}]}

User Object in mongo

{
    "_id" : ObjectId("53f3731c1401dacba49cfb74"),
    "userName" : "admin",
    "email" : "[email protected]",
    "sessionId" : "admin",
    "permissions" : [
        {
            "name" : "MPC",
            "link" : "#/Release",
            "sub" : []
        },
        {
            "name" : "MPC PE",
            "link" : "#",
            "sub" : [
                {
                    "name" : "Validate @ Release",
                    "link" : "#/mpcToPeRelease",
                    "sub" : []
                },
                {
                    "name" : "View Report",
                    "link" : "#/mpcToPeReport",
                    "sub" : []
                }
            ]
        }
    ]
}

User Class in java

@Document(collection="users")
public class User {

    private String id;

    private String userName;

    private String email;

    private String sessionId;

    private ArrayList<Permission> permissions;

    public String getUserName() {
        return userName;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public ArrayList<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(ArrayList<Permission> permissions) {
        this.permissions = permissions;
    }
}

and Permission Class in Java:

@Document
public class Permission {

    private String name;
    private String link;

    private ArrayList<Permission> subPermissions;

    public String getName() {
        return name;
    }

    public String getLink() {
        return link;
    }

    public ArrayList<Permission> getSubPermissions() {
        return subPermissions;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public void setSubPermissions(ArrayList<Permission> subPermissions) {
        this.subPermissions = subPermissions;
    }
}

Upvotes: 1

Views: 19667

Answers (1)

Ori Dar
Ori Dar

Reputation: 19000

Not sure it's the sole problem, but you should annotate the subPermissions field with @Field("sub") otherwise Spring maps it to a (non-existing) MongoDB field named subPermissions.

Also, note that annotating Permission class with Document is redundant since it's not really a first level document, but a sub-document. Anyway it's not an error.

Upvotes: 4

Related Questions