![](/img/trans.png)
[英]How do I add composite foreign key using Spring data JPA and Hibernate?
[英]Add foreign key using hibernate and jpa
這些是我的課程:
@Entity
@Table(name="assessment")
public class AssesmentProperties {
@Id
@Column(name="AssessmentId")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long AssessmentId;
@Column(unique=true,nullable=false)
private String AssessmentName;
private String AssessmentLevel;
private String Specialization;
private int time;
private String keywords;
private int NoOfSections;
//getters and setters
}
@Embeddable
public class SettingsPrimary implements Serializable {
private Long AssessmentId;
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long Section;
//getters and setters
}
@Entity
@Table(name="section")
public class SectionProperties {
@EmbeddedId
private SettingsPrimary PrimaryKey;
private String SectionType;
private int Weightage;
private int time;
private int NoOfQuestions;
//getters and setters
}
在表section
我需要將assessment_id
ID 創建為assessment
表的 FK 並在刪除時設置級聯。 我試圖用不同的方式來做到這一點,但沒有成功。
也許這可以幫助你。
@Entity
@Table(name="section")
public class SectionProperties {
@Id
private Long PrimaryKey;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "assessment_id", referencedColumnName="AssessmentId")
private AssesmentProperties AssesmentProperties;
private String SectionType;
private int Weightage;
private int time;
private int NoOfQuestions;
//getters and setters
}
我將 SectionProperties id 更改為Long
並將 AssessmentProprties 映射到 ManytoOne 道具。 這樣,綁定到 Section 的 AssessmentProperties 總是會被刪除,關聯的 SectionProperties 也會被刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.