1

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;
    }
}

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

whew can i find documentation abouy @field("sub")?
Its worth clarifying that @Field("sub") simply says that the field's storage in mongdb shall be under a key named "sub". The value "sub" is not magic.
You really saved me!!!! @Field helped me a lot. Thanks a lot mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.