英文:
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
创建为指向 assessment
表的外键,并在删除时设置级联操作。我尝试了不同的方法,但都没有成功。
英文:
Those are my classes:
@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
}
In the table section
I need to create assessment_id
as FK to assessment
table and set cascade on delete. I have tried to do it with different ways but without success.
答案1
得分: 1
或许这能帮助你。
@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
,并将AssessmentProperties映射为一个ManyToOne属性。这样,每当与某个Section绑定的AssessmentProperties被删除时,相关的SectionProperties也将被删除。
英文:
Maybe this could help you.
@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
}
I changed the SectionProperties id to a Long
and mapped the AssessmentProprties into a ManytoOne prop.
This way, always that a AssessmentProperties binded to a Section will be deleted, the associated SectionProperties will too.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论