在設定表關系方面需要幫助。因此,有 2 個物體 - Section 和 Period。1 Section 有很多 Period,但是1 Period 不屬于任何特定的 Section。我按如下方式實作了這種關系:在 Section 上創建了一個帶有外部鍵的附加表 SectionCodes (更多在圖中)

科類:
@Entity
@Table(name = "section")
@AttributeOverride(name = "id", column = @Column(name = "id", nullable = false))
public class Section extends BaseEntity<Integer> {
private String form;
private Integer version;
private List<SectionPeriod> periodCodes;
@Column(name = "form")
public String getForm() { return form; }
public void setForm(String form) { this.form = form;}
@Column(name = "version")
public Integer version() { return version; }
public void setVersion(Integer version) { this.version = version; }
@OneToMany(mappedBy = "section", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public List<SectionPeriod> getPeriodCodes() {
return periodCodes;
}
public void setPeriodCodes(List<SectionPeriod> periodCodes) {
this.periodCodes = periodCodes;
}
}
節期:
@Entity
@Table(name="section_period")
@AttributeOverride(name = "id", column = @Column(name = "id", nullable = false))
public class SectionPeriod extends BaseEntity<Integer> {
private Integer periodCode;
private Section section;
@Column(name = "period_code")
public Integer getPeriodCode() {
return periodCode;
}
public void setPeriodCode(Integer periodCode) {
this.periodCode = periodCode;
}
@ManyToOne()
@JoinColumn(name = "section_id", referencedColumnName = "id", nullable=false)
public Section getSection () {
return section;
}
public void setSection(Section section) {
this.section = section;
}
}
這作業正常,但有一個問題 - 事實證明,每個 Section 物體都有一個 SectionPeriod 串列,而 SectionPerid 有一個 Section - 所以存在回圈。有沒有簡單的方法來執行此操作?理想情況下,它應該只是 Section 類中的 List of Period 或至少是 periodCodes 的 Integer[]。
uj5u.com熱心網友回復:
要對 Period 和 Section 之間的多對多關系建模,您有兩種選擇:
- 使用 2 個物體(Period、Section),并在這些物體之一中使用 @ManyToMany 注釋。這將自動生成具有復合主鍵的連接表。不推薦這樣做。
- 使用 3 個物體,這樣您就可以將“ManyToMany”視為具有兩個 OneToMany 關系(1 個周期 - 許多“PeriodSection”和 1 個部分 - 許多“PeriodSection”)。建議這樣做,因為這樣您將擁有一個 PK 并且您可以輕松地向 PeriodSection 添加更多列(** 注意:物體和表應該有一個有意義的名稱,而不是 PeriodSection,但我將把它留給您)
使用第二個選擇:
時期:
@Entity
@Table(name = "periods")
public class Period {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer code;
private String name;
@OneToMany(mappedBy = "period", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private List<PeriodSection> periodSections; // bad naming.
}
期間部分:
@Entity
@Table(name = "period_section") // use a more meaningful name for the table and the entity
public class PeriodSection {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "period_id", referencedColumnName = "id")
private Period period;
@ManyToOne(cascade = CascadeType.MERGE)
@JoinColumn(name = "section_id", referencedColumnName = "id")
private Section section;
}
部分:
@Entity
@Table(name = "sections")
public class Section {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer version;
private String form;
@OneToMany(mappedBy = "section", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private List<PeriodSection> periodSections; // bad naming.
}
您可以嘗試使用 fetch 和級聯策略,以檢查哪個最適合您的應用程式。希望這有幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/389904.html
上一篇:如何從TeradataSQL中一列的值中獲取一些值?
下一篇:如何選擇不存在多個值的行
