gpt4 book ai didi

java - 如何在 mysql 数据库的 spring data jpa 中一起使用一对一和一对多关系

转载 作者:行者123 更新时间:2023-11-29 02:58:15 26 4
gpt4 key购买 nike

我在资源管理部门工作。在本模块中,我们将有下表

要求

请求编号

无职位

职位名称

req_recieved_time

需求状态

经验

技能

地点

资源

resId

姓名

经验

技能

地点

状态

可用

请求ID(fk)

需求或资源可以独立创建。我们有匹配屏幕,用户可以在其中映射与需求匹配的资源(匹配标准将是两个表中的技能和经验)。一旦用户选择并保存需求,资源的状态应该改变。每个资源都有状态 [Available, Shortlisted, In_progress, Background_Verification, On boarded, Rejected]。用户可以根据流程更改资源的状态。如果一个资源被拒绝,那么它应该可用于其他需求。为了为一个需求创建报告,有多少资源入围、有多少资源已上线以及有多少资源被拒绝,我们应该维护每个需求和资源状态。

那么数据应该是这样的

req_res_status
---------------

resId resId status updatedDate comment

1 1 shortlisted 18/12/19 candidate shortlisted
1 1 Inprogress 19/12/19 Done with one F2F
1 1 Rejected 20/12/19 Rejected in second round - F2F
1 2 shortlisted 18/12/19 candidate shortlisted
1 2 Inprogress 19/12/19 Done with one F2F
1 2 BGV_check 20/12/19 Background verification In progress
1 2 onboarded 01/01/20 Onboarded
2 1 shortlisted 20/12/19 candidate shortlisted
2 1 Inprogress 21/12/19 Done with one F2F
2 1 BGV_check 22/12/19 Background verification In progress
2 1 onboarded 01/01/20 Onboarded

一个需求可以有多个资源,一个资源可以关联一个需求

如何使用 spring data JPA 实现这一点以及如何维护关系。

我试过这种方式

资源.java

@Entity
public class Resource implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="res_id")
private int resourceId;

private String location;

private String name;

@Column(name="position")
private String jobTitle;

@Column(name="skills")
private String skill;

private String availability;

private String status;

private Integer experience;

//bi-directional many-to-one association to Requirement
@ManyToOne
@JoinColumn(name="req_fk")
private Requirement requirement;

// Setters & Getters

}

资源.java

@Entity
@NamedQuery(name="Requirement.findAll", query="SELECT r FROM Requirement r")
public class Requirement implements Serializable {
private static final long serialVersionUID = 1L;

@Id
//@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="req_id")
private String reqNumber;

@Column(name="acknowledge_status")
private String acknowledgeStatus;

@Column(name="acknowledge_time")
private String acknowledgeTime;

private int experience;

@Column(name="no_position")
private int noPositions;

@Column(name="req_recieved_time")
private String reqRecievedTime;

@Column(name="req_skills")
private String skill;

@Column(name="job_title")
private String position;

private String location;
@Column(name = "status")
private String status;

//bi-directional many-to-one association to Resource
@OneToMany(mappedBy="requirement")
private List<Resource> resources;

// setters & getters

}

最佳答案

@Entity
public class Resource implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="res_id")
private int resourceId;

private String location;

private String name;

@Column(name="position")
private String jobTitle;

@Column(name="skills")
private String skill;

private String availability;

private String status;

private Integer experience;

//bi-directional many-to-one association to Requirement

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "req_fk")
private Requirement requirement;

// Setters & Getters

}





@Entity
@NamedQuery(name="Requirement.findAll", query="SELECT r FROM Requirement r")
public class Requirement implements Serializable {
private static final long serialVersionUID = 1L;

@Id
//@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="req_id")
private String reqNumber;

@Column(name="acknowledge_status")
private String acknowledgeStatus;

@Column(name="acknowledge_time")
private String acknowledgeTime;

private int experience;

@Column(name="no_position")
private int noPositions;

@Column(name="req_recieved_time")
private String reqRecievedTime;

@Column(name="req_skills")
private String skill;

@Column(name="job_title")
private String position;

private String location;
@Column(name = "status")
private String status;

//bi-directional many-to-one association to Resource

@OneToMany(mappedBy = "requirement", cascade = CascadeType.ALL)
private List<Resource> resources;

// setters & getters

}

关于java - 如何在 mysql 数据库的 spring data jpa 中一起使用一对一和一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59423708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com