问题描述
这是我的实体类。
JobPost.java
@Entity
@Table(name = "job_post")
public class JobPost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "job_post_id")
private Long jobPostId;
@Column(name = "job_title")
private String jobTitle;
@Column(name = "job_description")
private String jobDescription;
@Column(name = "vacancy")
private int vacancy;
@Column(name = "posted_date")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date postedDate;
@Column(name = "total_applicants")
private int totalApplicants;
}
JobApplication.java
@Entity
@Table(name = "job_application")
public class JobApplication {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "job_application_id")
private Long jobApplicationId;
@Column(name = "job_post_id")
private Long jobPostId;
@Column(name = "applicant_id")
private Long applicantId;
}
Applicant.java
@Entity
@Table(name = "applicant")
public class Applicant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "applicant_id")
private Long applicantId;
@Column(name = "applicant_name")
private String applicantName;
@Column(name = "applicant_mobile_no")
private String applicantMobileNo;
@Column(name = "applicant_email")
private String applicantEmail;
}
我的主要目标是在 JobPostId 上列出 ApplicantList 。我对Spring数据JPA完全陌生。 JPA映射正确吗?我不知道我应该触发哪个查询才能获取基于jobPostId的applicistList。
解决方法
我建议使用JpaMappings并使用SpringData而不是使用本机查询。 遵循的步骤: 多对多:
使用JoinTable直接映射JobPost和Applicant,而不创建单独的类。
帮助链接: https://attacomsian.com/blog/spring-data-jpa-many-to-many-mapping
使用SpringData JPA findOne或findById方法(取决于Spring版本)。如果您使用EAGER fetch,则它将为您提供与JobPost ID相关的所有申请人。
一对多
保留JobApplication类并使用OneToMany注释。
帮助链接: https://attacomsian.com/blog/spring-data-jpa-one-to-many-mapping
查询:
@Query("select a from JobPost j inner join j.jobApplicantList ja inner join ja.applicant a where j.jobPostId=:jobPostId")
List<String> findAllJobApplicants(@Param("jobPostId") Long jobPostId);
,
我认为您应该以这种方式配置映射。为此,您只需要两个实体 JobPost.java
@Entity
@Table(name = "job_post")
public class JobPost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "job_title")
private String jobTitle;
@Column(name = "job_description")
private String jobDescription;
@Column(name = "vacancy")
private int vacancy;
@Column(name = "posted_date")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date postedDate;
@Column(name = "total_applicants")
private int totalApplicants;
@ManyToMany
@JoinTable(name = "applicant_job_post",joinColumns = {
@JoinColumn(name = "job_post_id",referencedColumnName = "id")
},inverseJoinColumns = {
@JoinColumn(name = "applicant_id",referencedColumnName = "id")
})
private Set<Applicant> applicants;
public JobPost() {
}
public void addApplicant(Applicant applicant) {
applicants.add(applicant);
applicant.getJobPosts().add(this);
}
public void removeApplicant(Applicant applicant) {
applicants.remove(applicant);
applicant.getJobPosts().remove(this);
}
}
Applicant.java
@Entity
@Table(name = "applicant")
public class Applicant {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "applicant_name")
private String applicantName;
@Column(name = "applicant_mobile_no")
private String applicantMobileNo;
@Column(name = "applicant_email")
private String applicantEmail;
@ManyToMany(mappedBy = "applicants")
private Set<JobPost> jobPosts;
public Applicant() {
}
public void addJobPost(JobPost jobPost) {
jobPosts.add(jobPost);
jobPost.getApplicants().add(this);
}
public void removeJobPost(JobPost jobPost) {
jobPosts.remove(jobPost);
jobPost.getApplicants().remove(this);
}
}