samedi 11 juin 2022

How to design User class

Suppose I have a requirement where I have to deal with users accessing my website. There can be two kinds of users an Admin and Normal user.

  1. Single user class that will be inherited by two child classes Admin and Normal. If so how to do this properly with JPA.
@Getter
@Setter
@Entity
@Inheritance
public class User{
    private String name;
    private String userId;
}
@Data
@Entity
public class Admin extends User{
    private String adminId;
}
@Data
@Entity
public class Normal extends User{

}
  1. One and only one User class and which will have an attribute stating the role of that user.

Which will be the best design strategy and if we are looking towards inheritance should we persist those in a single table or multiple tables with a super table or should the parent not have a table and details should be persisted along with the sub tables using mapped super class.

Also if we shouldn't use inheritance then where should we probably use that and what should be the usecase and vice versa.

Aucun commentaire:

Enregistrer un commentaire