简体   繁体   中英

How to use one-one and one -many relationships together in spring data jpa for mysql database

I am working for Resource management. In this module we will have below tables

requirement

reqId

no_of_postions

job_title

req_recieved_time

requirement_status

experience

skill

location

resource

resId

name

experience

skill

location

status

available

reqId(fk)

Requirement or Resource can be created independently.We have Matching screen where user can map the Resources which are matching to the Requirement (matching criteria will be skill and experience from both tables). Once user selects and save the Requirement, the status of the Resource should change. Each Resource is having statuses [Available, Shortlisted, In_progress, Background_Verification, On boarded, Rejected] . User can change the status of the Resource-based on the process. If one Resource is rejected then it should be available for the other Requirements. For creating the reports for one requirement how many resources are shortlisted, how many resources are onboard and how many resources are rejected we should maintain each requirement and resource status.

Then the data should be like this

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 

One requirement can have multiple resources, One resource can be associate to one requirement

How to achieve this using spring data JPA and how to maintain the relationships.

I have tried this way

Resource.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

}

Resource.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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM