2个表之间的多重关系,如何设计通过hibernate与数据库映射的类模型

时间:2018-12-26 13:57:50

标签: java oracle hibernate

我有2个表项目,并且员工有多个关系。项目有很多员工,员工可以参加很多(很多)项目。一个项目有一名员工是团队负责人,一名员工可以管理多个项目(多对一个)。因此,我如何设计数据库,并使用数据库进行类模型映射。谁来帮帮我 这是我的代码

班级用户

@Entity(name = "USERS")
public class Users {
@Id
@Column(name = "USER_ID", length = 6)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
@Column(name = "USER_NAME", length = 50, nullable = false)
private String userName;
@Column(name = "PASS_WORD", length = 50, nullable = false)
private String passWord;
@Column(name = "FULL_NAME", length = 50, nullable = false)
private String fullName;
@Column(name = "EMAIL", length = 50, nullable = false)
private String email;
@Column(name = "PHONE", length = 11, nullable = true)
private String phone;
@Column(name = "STATUS", nullable = true)
private Boolean status;
@ManyToMany(fetch = FetchType.LAZY)
@JsonIgnore
@JoinTable(name = "USERPROJECT", joinColumns = { @JoinColumn(name = 
 "USER_ID") }, inverseJoinColumns = {
        @JoinColumn(name = "PROJECT_ID") })
private List<Project> projects;
@OneToMany(mappedBy = "teamlead")
private List<Project> projectOfTeamLead;
//get set.....
}

班级项目

    @Entity(name = "PROJECTS")
    public class Project {
    @Id
    @Column(name = "PROJECT_ID", length = 6)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer projectId;
    @Column(name = "PROJECT_NAME", length = 50, nullable=false)
    private String projectName;
    @Column(name = "USER_CREATE_ID", length = 6, nullable=false)
    private Integer userCreateId;
    //  @Column(name = "TEAM_LEAD_ID", length = 6, nullable=false)
    //  private Integer teamLeadId;
    @Column(name = "TIME_START", nullable=true)
    private Date timeStart;
    @Column(name = "TIME_END", nullable=true)
    private Date timeEnd;
    @ManyToMany(mappedBy = "projects")
    private List<Users> users;
    @ManyToOne
    @JoinColumn(name = "TEAM_LEAD_ID")
    private Users teamlead;
    //get set...
    }

1 个答案:

答案 0 :(得分:1)

我认为您需要使数据库适合3nf(第三范式)。您正在描述一个使用两个表的数据库,但我认为您至少需要三个表,这样您就可以描述哪个员工在哪个项目上。应该使用一个使用其他两个表中的PK的“团队”表。通常,您将其命名为“ ProjectEmployees”,因为它分别由“ Projects”表和“ Employees”表中的PK组成。我希望能回答您的问题。如果您不明白我在说什么,我强烈建议您观看this