我有 3 個表:員工、估算和任務。
- 每個員工可能有許多插補,一個插補一次只能屬于一名員工(ManyToOne 關系)
- 列出每個任務可能有多個插補,一個插補一次只能屬于一個任務(ManyToOne 關系
- 我希望插補表將 TASK_ID 作為其 PRIMARY_KEY
這是我的 3 個物體:
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="employee")
public class AppUser {
@Id @GeneratedValue(strategy = IDENTITY)
private Integer id;
private String first_name;
private String last_name;
@OneToMany(mappedBy="employee")
private List<Imputation> imputations;
}
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Imputation {
@Id
private String id; //something needs to be done here to have the id = task_id
private LocalDate day;
private Double workload;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name= "employee_id")
private AppUser employee;
}
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class Task {
@Id
@GeneratedValue(strategy = SEQUENCE)
private Long id;
private String name;
private String description;
@OneToMany(mappedBy = "task")
private List<Imputation> imputations;
}
這就是我希望我的表插補的樣子(正如我所說,task_id 是主鍵和外鍵):
task_id | day | workload |employee_id
uj5u.com熱心網友回復:
嘗試:
@Entity
public class Imputation {
@Id
@OneToOne
@JoinColumn(name= "task_id")
private Task task;
private LocalDate day;
private Double workload;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name= "employee_id")
private AppUser employee;
}
或者,如果您想在物體中保留字串屬性:
@Entity
public class Imputation {
@Id
private String id;
@MapsId
@OneToOne
@JoinColumn(name= "task_id")
private Task task;
private LocalDate day;
private Double workload;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name= "employee_id")
private AppUser employee;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/473260.html
