Jpa 中的一对多关系

One-Many Relationship in Jpa

我想从具有 1-M 关系的实体中获取数据。 用户有一个 cv information.With JpaRepo 的实体, 简历 class :

@Entity
@Table(name = "cvs")
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler", "educations", "works", "langueges", "technologies"})
public class CV {

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

    //ToDo : Employee bilgilerinin görünmesi problemi giderilecek.
    @OneToOne
    @JoinColumn(name = "employee_id")
    private Employee employee;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Education> educations;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Work> works;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Languege> langueges;

    @OneToMany(mappedBy = "cv", cascade = CascadeType.ALL, orphanRemoval = true)
    List<Technology> technologies;

    @Column(name = "github")
    private String github;

    @Column(name = "linkedin")
    private String linkedin;

    @NotNull
    @NotBlank
    @Column(name = "cover_letter")
    private String coverLetter;

    @Column(name = "photo")
    private String photo;
    
}

这是教育class(工作、语言、技术class相同):

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "cv_educations")
public class Education {

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

    @NotNull
    @NotBlank
    @Column(name = "school_name")
    private String schoolName;

    @NotNull
    @NotBlank
    @Column(name = "department")
    private String department;

    @NotNull
    @NotBlank
    @PastOrPresent
    @Column(name = "starting_date")
    @DateTimeFormat(pattern = "yyyy-mm-dd")
    private LocalDate startingDate;

    @NotBlank
    @Column(name = "graduation_date")
    @DateTimeFormat(pattern = "yyyy-mm-dd")
    private LocalDate graduationDate;

    @ManyToOne
    @JoinColumn(name = "cv_id")
    private CV cv;
}

我尝试用 jpa 构建以下结构,但构造函数采用列表参数。我得到一个错误,因为我不能用 jpql

来写
public interface CvRepository extends JpaRepository<CV, Integer> {

        @Query("select new com.demo.humanresourcesmanagementsystem.Entities.concretes.CV" +
                "(employee.firstName, employee.lastName, cv.github, cv.linkedin, cv.coverLetter," +
                "educations, works, langueges, technologies)" +
                "from CV cv inner join cv.employee employee inner join cv.educations educations " +
                "inner join cv.works works inner join cv.langueges langueges " +
                "inner join cv.technologies technologies where cv.employee.id =:employeeId")
        CV findByCv(int employeeId);
    }

我想了解这个实体中的教育、作品、语言和技术。也就是说输出的是一个cv,但是cv里面可能有多个教育对象(比如小学,高中),传入的数据会是下面的格式,例如:

"firstName": "X",
"lastName" : "X",
"educations" : [
           "education1" {
                  "school" : "x", 
                  "department" : "x" ...}, 
           "education2" {
                  "school" : "x", 
                  "department" : "x"...}
"works" : [
    "work1" {
           "workplace" : "x", 
           "job" : "x" ...
          }
       ]
"github" : "x",
"linkedin" : "x"

如何使用 jpa 存储库设置此结构?如果我要使用它,我应该写什么样的 dto?谢谢。

更新

当我使用 spring jpa 派生查询 (findByEmployeeId) 时,我收到的数据格式如下:

{
  "success": true,
  "message": "string",
  "data": {
    "id": 0,
    "employee": {
      "id": 0,
      "email": "string",
      "password": "string",
      "firstName": "string",
      "lastName": "string",
      "nationalIdentity": "string",
      "yearOfBirth": 0
    },
    "github": "string",
    "linkedin": "string",
    "coverLetter": "string",
    "photo": "string"
  }
}

所以我无法接收有关教育、工作、语言和技术的数据。

您似乎正试图通过 employer.id 检索简历。在那种情况下,您真的可以只使用包含关键字的 JPA 查询方法。在这种情况下,它看起来像:

CV findByEmployeeId(int employeeId);

这应该是 return 完整的 CV 对象,正如您所期望的那样。

有关 JPA 查询方法关键字的更多详细信息,请参阅 here