[英]One-Many Relationship in Jpa
我想從具有 1-M 關系的實體獲取數據。 用戶有一個 cv 信息的實體。使用 JpaRepo,Cv 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(工作、語言、技術課程相同):
@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為output,但是cv里面可能有不止一個學歷object(比如小學,高中),傳入的數據會是下面的格式,例如:
"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);
這應該會如您所料返回完整的CV
object。
有關 JPA 查詢方法關鍵字的更多詳細信息,請參見此處。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.