使用Hibernate和JPA添加外键。

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

Add foreign key using hibernate and jpa

问题

以下是翻译好的内容:

这些是我的类:

  1. @Entity
  2. @Table(name="assessment")
  3. public class AssesmentProperties {
  4. @Id
  5. @Column(name="AssessmentId")
  6. @GeneratedValue(strategy=GenerationType.IDENTITY)
  7. private Long AssessmentId;
  8. @Column(unique=true,nullable=false)
  9. private String AssessmentName;
  10. private String AssessmentLevel;
  11. private String Specialization;
  12. private int time;
  13. private String keywords;
  14. private int NoOfSections;
  15. //getters and setters
  16. }
  17. @Embeddable
  18. public class SettingsPrimary implements Serializable {
  19. private Long AssessmentId;
  20. @GeneratedValue(strategy=GenerationType.IDENTITY)
  21. private Long Section;
  22. //getters and setters
  23. }
  24. @Entity
  25. @Table(name="section")
  26. public class SectionProperties {
  27. @EmbeddedId
  28. private SettingsPrimary PrimaryKey;
  29. private String SectionType;
  30. private int Weightage;
  31. private int time;
  32. private int NoOfQuestions;
  33. //getters and setters
  34. }

在表格 section 中,我需要将 assessment_id 创建为指向 assessment 表的外键,并在删除时设置级联操作。我尝试了不同的方法,但都没有成功。

英文:

Those are my classes:

  1. @Entity
  2. @Table(name="assessment")
  3. public class AssesmentProperties {
  4. @Id
  5. @Column(name="AssessmentId")
  6. @GeneratedValue(strategy=GenerationType.IDENTITY)
  7. private Long AssessmentId;
  8. @Column(unique=true,nullable=false)
  9. private String AssessmentName;
  10. private String AssessmentLevel;
  11. private String Specialization;
  12. private int time;
  13. private String keywords;
  14. private int NoOfSections;
  15. //getters and setters
  16. }
  17. @Embeddable
  18. public class SettingsPrimary implements Serializable {
  19. private Long AssessmentId;
  20. @GeneratedValue(strategy=GenerationType.IDENTITY)
  21. private Long Section;
  22. //getters and setters
  23. }
  24. @Entity
  25. @Table(name="section")
  26. public class SectionProperties {
  27. @EmbeddedId
  28. private SettingsPrimary PrimaryKey;
  29. private String SectionType;
  30. private int Weightage;
  31. private int time;
  32. private int NoOfQuestions;
  33. //getters and setters
  34. }

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

或许这能帮助你。

  1. @Entity
  2. @Table(name="section")
  3. public class SectionProperties {
  4. @Id
  5. private Long PrimaryKey;
  6. @ManyToOne(cascade = CascadeType.ALL)
  7. @JoinColumn(name = "assessment_id", referencedColumnName="AssessmentId")
  8. private AssesmentProperties AssesmentProperties;
  9. private String SectionType;
  10. private int Weightage;
  11. private int time;
  12. private int NoOfQuestions;
  13. // getters and setters
  14. }

我将SectionProperties的id更改为Long,并将AssessmentProperties映射为一个ManyToOne属性。这样,每当与某个Section绑定的AssessmentProperties被删除时,相关的SectionProperties也将被删除。

英文:

Maybe this could help you.

  1. @Entity
  2. @Table(name="section")
  3. public class SectionProperties {
  4. @Id
  5. private Long PrimaryKey;
  6. @ManyToOne(cascade = CascadeType.ALL)
  7. @JoinColumn(name = "assessment_id", referencedColumnName="AssessmentId")
  8. private AssesmentProperties AssesmentProperties;
  9. private String SectionType;
  10. private int Weightage;
  11. private int time;
  12. private int NoOfQuestions;
  13. //getters and setters
  14. }

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:

确定