0

I'm working on an application using Spring Boot , so the UML class i was working with had 3 Entities :

  1. Admin(id,username,password,Email)
  2. Manager(id,username,password,Email,Phone)
  3. Client(id,username,password,Email,phone,Adresse)

the Admin has a mapping of one to many/ many to one with Manager also one to many/many to one with the entity Client .

After some researches i changed this to User/Role System , so the entity User has attributes like this :

    public class AppUser implements Serializable {

    @Id @GeneratedValue
    private Long id;
    @Column(unique = true)
    private String username;
    private String password;
    @ManyToMany(fetch = FetchType.EAGER)
    private Collection<AppRole> roles = new ArrayList<>();
}

And the Role Entity

 public class AppRole implements Serializable {

        @Id @GeneratedValue
        private Long id;
        private String roleName;

}

What should i do to implement the others entities , Manager and Client as they have other different attributes ?

and how i should implement the mapping now as there is only one Entity ?

thank you in advance.

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
dEs12ZER
  • 788
  • 4
  • 24
  • 51
  • What is this? An UML question or an implementation question? – qwerty_so Apr 20 '18 at 17:20
  • @ThomasKilian thank you for your answer first , it's an UML question so i can implement these classes by the end in my code , i wanted to explain my question with the implementation of these classes. – dEs12ZER Apr 20 '18 at 18:39
  • Your question is not clear, please explain more. – manuka_m Apr 20 '18 at 19:28
  • @manuka_m okey let me explain to you : i was working with three entities Admin , Manager , Client , they don't have the same attributs as in the post , after that i decided to use two entities : AppUser and AppRole instead of using three entities ( working with spring security everthing goes well for roles etc.. ) , in the post you can see that i've declared only three attributs id,username,password , so should i add the other attributs of manager and client in entity appUser? Or should i created two entities manager and client heritating the class appUser ? – dEs12ZER Apr 20 '18 at 20:29
  • Another question what about the ORM mappig that i was working with (between admin and manager , also admin and client) , the admin can add/remove manager/client .. – dEs12ZER Apr 20 '18 at 20:31
  • You can create two separate entities for manager and client and extend appuser class from those two entities. That way common attributes of the manager and client will be in the appuser class and specific attributes will be in the two separate entities – manuka_m Apr 20 '18 at 20:35
  • For the mapping, you can use onetomany mapping between appuser and approle. Appuser has one approle and oneapprole can have many appusers. – manuka_m Apr 20 '18 at 20:37
  • Rather than adding comments you should edit your question to clarify it. – qwerty_so Apr 20 '18 at 20:45
  • @Thomas Kilian you're absolutely right , i will edit the post – dEs12ZER Apr 20 '18 at 20:55
  • @manuka_m : thank you for your help ,this is what i will do , for mapping i meant between the users , i mean admin who can add or remove a client etc , should i add one to many to the appuser itself ? Or there is no need ? – dEs12ZER Apr 20 '18 at 20:58

1 Answers1

1

I think you should add those fields in AppUser class and insert data when required. For example while creating Manager you can enter admin_id and leave client_id blank. This is how one admin can also have multiple managers under him.

@Entity
@Table(name = "AppUser")
public class AppUser implements Serializable {

    @Id @GeneratedValue
    private Long id;
    @Column(unique = true)
    private String username;
    private String password;
    @Column(unique = true)
    private String email;
    @Column(unique = true)
    private String phone;
    private String address;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_role", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private Collection<AppRole> roles = new ArrayList<>();
    @ManyToOne
    @JoinColumn(name = "client_id")
    private AppUser client;
    @ManyToOne
    @JoinColumn(name = "manager_id")
    private AppUser manager;
    @ManyToOne
    @JoinColumn(name = "admin_id")
    private AppUser admin;
}

And then you can give respective roles to each appUser as per your requirement. If you are not already using Spring Security then you should use it for roles and permissions.

UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35
  • thank you for your answer first , and yes i'm already using Spring Security , i have an Entity roles which gives respective roles to each appUser , i have one more question , to insert data into this Entity i use an interface extending JPaRepository , so how i'm going to specify that this user is an admin etc ? should i create my own method in this interface to specify which user is that ? and does your solution better than creating two entities extending the app user entity ? thank you in advance. – dEs12ZER Apr 22 '18 at 10:59
  • Yes `JpaRepository` will do the work as it already has `CRUD` methods. What you need to do is 1) add 3 roles in your `AppRole` class then 2) assign one role to user and 3) save. something like this: `AppRole role = roleRepo.findByRoleName("ROLE_ADMIN"); Collection roles = new ArrayList<>(); roles.add(role); user.setRoles(roles); userRepo.save(user);` – UsamaAmjad Apr 22 '18 at 20:57
  • Thank you so much for your help , what about the mapping , is it one to many or many to one ? As for exemple the admin will have multiple managers under him . – dEs12ZER Apr 22 '18 at 22:35
  • If it helps then kindly accept the answer. You need to understand the diff between these two, kindly check the https://stackoverflow.com/a/19924648/4704510 – UsamaAmjad Apr 23 '18 at 10:16
  • Thank you for your help , i appreciate this a lot :) – dEs12ZER Apr 23 '18 at 11:39