如何为 3 个不同的角色设计 类 in spring boot?

How to design classes in spring boot for 3 different roles?

我正在开发处理分发管理的 spring 启动应用程序。有 3 种类型的用户将与系统交互 - 管理员、分销商和零售商。

  1. 一个公司只能有一个管理员。
  2. 一个公司在一个城市只能有一个经销商。
  3. 零售商可以链接到不同公司的经销商 特定城市的。

现在,对于这些东西。我创建了 5 类 - 用户、角色、管理员、分销商和零售商。 任何人都可以建议我在设计所需 类 方面应该如何解决这个问题。我最初尝试过,但我仍然认为这个 类 结构本身很复杂。是否有我遗漏的东西,或者是否有任何设计模式可以使事情变得更加不稳定??

@Entity
@Table(name = "tbl_User")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "user_id")
    private Long userId;
    
    @NotNull(message = "first Name cannot be null")
    @Column(name = "first_Name")
    private String firstName;
    
    @NotNull(message = "last Name cannot be null")
    @Column(name = "last_Name")
    private String lastName;
    
    @ManyToOne()
    @JoinColumn(name = "role_Id")
    private Role role;
    
    @NotNull
    @Size(min = 10, max = 10, message = "Contact must be of 10 characters")
    @Column(name = "contact")
    private String contact;
    
    @NotNull
    @Email(message = "Email should be valid")
    @Column(name = "email", unique = true)
    private String email;
    
    @NotNull
    @Size(min = 10, max = 255, 
    message = "Local address must be between 10 and 255 characters")
    @Column(name = "local_Address")
    private String localAddress;
    
    @ManyToOne()
    @JoinColumn(name = "city_Id")
    private City city;
    
    @NotNull
    @Column(name = "user_Name")
    private String userName;
    
    @NotNull
    @Column(name = "password")
    private String password;
    
    @Column(name = "registered_On")
    private LocalDateTime registeredOn;
    
    @Column(name = "updated_On")
    private LocalDateTime updatedOn;
    
    @Column(name = "approved_By")
    private Long approvedBy;
    
    @NotNull
    @Size(min = 1, max = 1, 
    message = "Approval status must be of 1 character.")
    @Column(name = "approval_Status")
    private String approvalStatus;

    @NotNull
    @Column(name = "active_Status")
    private boolean activeStatus;
    
    @Transient 
    private Company company;
}
@Entity
@Table(name = "tbl_Administrator")
public class Admin {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private int adminId;
    
    @OneToOne()
    @JoinColumn(name = "user_Id")
    private User user;
    
    @OneToOne()
    @JoinColumn(name = "company_Id")
    private Company company;    
}
@Entity
@Table(name = "tbl_Distributor")
public class Distributor {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long distributorId;
    
    @OneToOne()
    @JoinColumn(name = "user_Id")
    private User user;
    
    @ManyToOne()
    @JoinColumn(name = "company_Id")
    private Company company;
}
@Entity
@Table(name = "tbl_Retailer")
public class Retailer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long retailerId;

    @OneToOne()
    @JoinColumn(name = "user_Id")
    private User user;
}

我会选择 @MappedSuperclass 而不是制作 User 和实体本身。基本上,AdminDistributorRetailer 会扩展这个映射的 superclass。为每个创建一个单独的 table,但没有为 superclass (User) 本身创建 table。

@MappedSuperclass
public class User {

   // Your properties
   
}
@Entity
@Table(name = "tbl_Administrator")
public class Admin extends User {

    @OneToOne()
    @JoinColumn(name = "company_Id")
    private Company company;  
     
}
@Entity
@Table(name = "tbl_Distributor")
public class Distributor extends User {
    
    @ManyToOne()
    @JoinColumn(name = "company_Id")
    private Company company;
    
}
@Entity
@Table(name = "tbl_Retailer")
public class Retailer extends User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    private Long retailerId;
    
}

您还有其他可能性(在 https://medium.com/analytics-vidhya/jpa-hibernate-entity-inheritance-1f6aa7ea2eea 阅读更多内容),例如:

  • 所有实体的单个 table
  • 将 superclass 和 subclass 分开 table。在 superclass 中定义的属性不包含在 subclass.
  • 的 table 中
  • Table per class,类似于Mapped Superclass,不同之处在于Superclass也有对应的table。

不过,我还是会按照描述使用 Mapped Superclass。