我应该使用哪种表关系:hibernate

时间:2017-12-01 09:55:59

标签: hibernate spring-boot many-to-many one-to-many table-relationships

我正在使用身份验证机制处理spring-boot应用程序。 我有一种情况, 用户将来自不同的公司单位,具有单独的本地管理员。应根据角色限制用户访问某些页面。此外,只有部分用户可以访问公司单位的页面。

需要对每个页面上的每个角色应用单独的权限(创建,更新,删除)集。 角色应由管理员用户

创建

截至目前,我有6个表格实体。

  1.     @Entity
        @Table(name="SQA_pages")
        public class Pages {
        // These are the table columns 
        @Id
        @GeneratedValue( strategy=GenerationType.AUTO )
        @OneToMany(mappedBy = "page" )
        private int id;
        private String pageName;
    

    }

  2. 公司单位

        public class CompanyUnitDetails {
        @Id
        @GeneratedValue( strategy=GenerationType.AUTO )
        @OneToMany( mappedBy = "companyUnit" )
        private int id;
        private String unitName;
        private String databaseName;
        private String username;
        private String password;
        private String host;
        private String portnumber;
    

    }

  3. 3.UserDetails

    public class UserDetails {
        @Id
        @GeneratedValue( strategy=GenerationType.AUTO )
        private long id;
        private String name;
        private String username;
        private String password;
        private String emailId;
        private String domain;
        private String createdBy;
        private Date creationDate;
        ------>companyId<------
        @ManyToOne(cascade=CascadeType.ALL)
        @JoinColumn(name="unit_id",nullable=false)
        private CompanyUnitDetails unitDetails;
    }
    

    4.Roles

    public class Roles {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @OneToMany(mappedBy = "role" )
    
        private int id;
        private String roles;
        private String createdBy;
        private Date creationDate;
        --->companyid<-----
        @ManyToOne(cascade = CascadeType.ALL)
        @JoinColumn(name = "company_unit_id", nullable = false)
        private CompanyUnitDetails companyUnit;
    }
    

    5.Role-PageMapping

    public class RolePageMapping {
    
        @Id
        private int id;
        private boolean writePermission;
        private boolean updatePermission;
        private boolean deletePermission;
        private String createdBy;
        private Date creationDate;
        -->pageId<--
        @ManyToOne(cascade=CascadeType.ALL)
        @JoinColumn(name="page_id",nullable=false)
        private Pages page;
        -->roleId<--
        @ManyToOne(cascade=CascadeType.ALL)
        @JoinColumn(name="role_id",nullable=false)
        private Roles role;
    }
    

    6.User-RoleMapping

    public class UserRoleMapping {
    
        @Id
        private long id;
        @ManyToOne
        private UserDetails users;
        @ManyToOne
        private Roles role;
        }
    

    我的实际需要是

    1.Page Id PK应该从页面角色映射中引用

    2.unit Id将从角色和用户详细信息中引用。

    3.role _id将从页面角色映射和用户角色映射中引用。

    但我很困惑,我该怎么做?如何从两个表中引用一个主键..? 实际上是否需要多对多映射?

0 个答案:

没有答案