1

I am trying to implement Spring Boot (User-by-Role) authentication with MongoDB.

I have done so in SQL (such as MySQL) using (User-Role) Many-to-many relationship like this way with annotations.

So how NoSQL databases maintain the relationship? I found this article. According the article, Some database such as Oracle and PostgreSQL have support for array data type. Therefore the roles of a user should be attached/bounded with user object as arrays of Role. I also came across another fantastic article to implement my idea.

Lets have a look on code I have made ...

Role (Java code + Mongo data):

@Document(collection = "roles")
public class Role {

    @Id private String id;
    private String name;

    // getters & setters

}
------------------------------------------------------------------------------------------
{"_id" : ObjectId("58b44a2b2f21472b5457f601"), "name" : "ROLE_ADMIN"}
{"_id" : ObjectId("58b44a3c2f21472b5457f602"), "name" : "ROLE_USER"}

User (Java code + Mongo data): Here, I use reference(s) rather than value(s) of Role (that is ROLE_ADMIN, ROLE_USER) because in future value(s) may change ...

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

    @Id private String id;
    private String userName;
    private String password;
    private boolean enabled;

    private Set<Role> roles;

    // getters & setters

}
------------------------------------------------------------------------------------------
{"_id" : ObjectId("58b2fdaf2f214709447f635d"), "userName" : "johir1", "password" : "$2a$04$S8edObuDygOMm0ccPfu2T.CsDgqDygTXW2A8adAsTRmtdmJehURQu", "enabled" : true, "roles" : ["58b44a2b2f21472b5457f601"]}
{"_id" : ObjectId("58b4c21e00ecf75512594fa1"), "userName" : "johir2", "password" : "$2a$04$mgHMTC2FrDUgpU3sNi3d6uRKl0zgRO6bO55aD5R8dCTVZYPtJTPtW", "enabled" : true, "roles" : ["58b44a2b2f21472b5457f601", "58b44a2b2f21472b5457f602"]}

Problems:

I would like to establish relationship between User & Role as the following code snippet

    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
    private Set<User> users;
    ---------------------------------------------------------------
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Set<Role> roles;

How do I do that while NoSQL database has tables? To be very specific, how NoSQL establish relationship between objects?

Community
  • 1
  • 1
testuser
  • 793
  • 3
  • 13
  • 35
  • 2
    MongoDB is *not* a Relational Database Management System, so it's not a very good idea to keep the RDBMS way of thinking when you're working with an entirely different paradigm. – Kayaman Feb 28 '17 at 15:42
  • 1
    This article may help: https://www.tutorialspoint.com/mongodb/mongodb_data_modeling.htm Instead of having role, user, and user & role, consider using one single collection for your authentication – SL07 Feb 28 '17 at 17:26

0 Answers0