使用hibernate和jpa添加外键

2nbm6dog  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(324)

这些是我的课程:

@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
}

在table上 section 我需要创造 assessment_id 作为fk收件人 assessment 表并在删除时设置cascade。我试过用不同的方法做,但没有成功。

svujldwt

svujldwt1#

也许这能帮你。

@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 把评估属性Map成一个多对一的属性。这样,绑定到节的assessmentproperties总是会被删除,关联的节属性也会被删除。

相关问题