Reputation: 848
i'm working on a simple application to learn how to work with spring boot/hibernate , and i'm using spring security with system of Role/Users , so i have an entity called User , another one called Roles .
for my application i have two types of users :
Admin with attributes ( id , username , password)
client with attributes (id , username , password , email , phoneNumber)
that's why i decided to use the concept of inheritance in spring data with creating an Entity User with attributes (id,username,password) and the Client Entity which inherites the Entity User and has the others attributes(id,email,phoneNumber).
in this app , the client can add/remove/ .. some products that exists in my database Mysql , so i wanted to implement a mapping of (one to many / many to one) between entity client and entity product.
now how should i implement this (ORM SIDE), i mean should i do the mapping between entity Client and Product OR between User and Product?
if there is any other better solution i'll be so thankfull :) .
EDIT :
@Entity
@Data
@AllArgsConstructor @NoArgsConstructor
public class User implements Serializable {
@Id @GeneratedValue
private Long id;
@Column(unique = true)
private String username;
private String password;
private String prenom;
private String nom;
private Long Tel;
private String CIN;
private String Email ;
@ManyToMany(fetch = FetchType.EAGER)
private Collection<AppRole> roles = new ArrayList<>();
@ManyToOne
@JoinColumn(name="client_id")
private User Client;
@ManyToOne
@JoinColumn(name = "manager_id")
private User Manager;
}
Upvotes: 1
Views: 834
Reputation: 2121
You should have a single table 'user' and 2 roles 'ADMIN' and 'CLIENT'. I would recommend that both user types should have same details including email address and phone number. So when the user logs in, you can load user roles/granted authorities from userdetails service.
EDIT: Your user table should look like
id , username , password , email , phoneNumber, added_by_id
the added_by_id is a self join and references to the user.id column.
so in your entity class, you will do something like the following.
...
@ManyToOne
@JoinColumn(name = "added_by_id")
protected User addedBy;
Upvotes: 2