使用Hibernate和JPA添加外键。

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月6日 14:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63761364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定