Constantly getting org.hibernate.AnnotationException: @OneToOne or @ManyToOne references an unknown entity: error

huangapple 未分类评论44阅读模式
英文:

Constantly getting org.hibernate.AnnotationException: @OneToOne or @ManyToOne references an unknown entity: error

问题

我可以成功地通过Db2控制台将记录插入到表中。

  1. INSERT INTO db2admin.school VALUES (1, 'BJS');
  2. INSERT INTO db2admin.STUDENT VALUES (1, 'Pankaj', 30, 1);

我有以下的表。

  1. CREATE TABLE DB2ADMIN.School (school_id SMALLINT PRIMARY KEY NOT NULL, name varchar(128));
  2. CREATE TABLE DB2ADMIN.Student (student_id int PRIMARY KEY NOT NULL, name varchar(128) NOT NULL, age smallint NOT NULL, school_id int NOT NULL, FOREIGN KEY (school_id) REFERENCES School(school_id));

一个学校可以包含许多学生。以下是我在Hibernate 5.4.18中如何定义实体关系。

  1. @Entity
  2. @Table(name = "school")
  3. public class School {
  4. @Id
  5. @SequenceGenerator(name = "SchoolIdSequence", schema = "db2admin", sequenceName = "Sequence_School", allocationSize = 1)
  6. @GeneratedValue(generator = "SchoolIdSequence", strategy = GenerationType.SEQUENCE)
  7. @Column(name = "SCHOOL_ID")
  8. private int id;
  9. @Column(name = "NAME")
  10. private String name;
  11. @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "school")
  12. //@JoinColumn(name = "student_id", nullable = false)
  13. private Set<Student> students;
  14. public School() {}
  15. public School(String name) {
  16. this.name = name;
  17. }
  18. // ... 省略其他方法和属性的定义
  19. }
  20. @Entity
  21. @Table(name = "student")
  22. public class Student {
  23. @Id
  24. @SequenceGenerator(name = "SequenceStudentId", sequenceName = "Sequence_Student", allocationSize = 1)
  25. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceStudentId")
  26. @Column(name = "STUDENT_ID")
  27. private int id;
  28. @Column(name = "NAME")
  29. private String name;
  30. @Column(name = "AGE")
  31. private int age;
  32. @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
  33. @JoinColumn(name = "school_id", nullable = false)
  34. private School school;
  35. public Student() {}
  36. public Student(String name, int age, School school) {
  37. this.name = name;
  38. this.age = age;
  39. this.school = school;
  40. }
  41. // ... 省略其他方法和属性的定义
  42. }

在执行应用程序时,我收到以下错误。

  1. org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.jfa.persistence.hibernate.entity.Student.school references an unknown entity: org.jfa.persistence.hibernate.entity.School

我已经尝试根据Internet/StackOverflow上其他人的建议以不同的方式调整类中的关系设置。然而,到目前为止还没有成功。请帮忙解决。

英文:

I can successfully insert records into the tables using the Db2 console.

  1. INSERT INTO db2admin.school VALUES (1, &#39;BJS&#39;);
  2. INSERT INTO db2admin.STUDENT VALUES (1, &#39;Pankaj&#39;, 30, 1);

I have the following tables.

  1. CREATE TABLE DB2ADMIN.School (school_id SMALLINT PRIMARY KEY NOT NULL, name varchar(128));
  2. create table DB2ADMIN.Student (student_id int PRIMARY KEY NOT null, name varchar(128) not null, age smallint not null, school_id int NOT NULL, FOREIGN KEY (school_id) REFERENCES School(school_id));

One School can contain many students. Here is how I am defining the entity relationships in Hibernate 5.4.18.

  1. @Entity
  2. @Table(name = &quot;school&quot;)
  3. public class School
  4. {
  5. @Id
  6. @SequenceGenerator(name = &quot;SchoolIdSequence&quot;, schema=&quot;db2admin&quot;, sequenceName = &quot;Sequence_School&quot;, allocationSize = 1)
  7. @GeneratedValue(generator = &quot;SchoolIdSequence&quot;, strategy = GenerationType.SEQUENCE)
  8. @Column(name = &quot;SCHOOL_ID&quot;)
  9. private int id;
  10. @Column(name = &quot;NAME&quot;)
  11. private String name;
  12. @OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy = &quot;school&quot;)
  13. //@JoinColumn(name=&quot;student_id&quot;, nullable=false)
  14. private Set&lt;Student&gt; students;
  15. public School()
  16. {}
  17. public School(String name)
  18. {
  19. this.name = name;
  20. }
  21. public int getId()
  22. {
  23. return id;
  24. }
  25. public void setId(int id)
  26. {
  27. this.id = id;
  28. }
  29. public String getName()
  30. {
  31. return name;
  32. }
  33. public void setName(String name)
  34. {
  35. this.name = name;
  36. }
  37. public Set getStudents()
  38. {
  39. return students;
  40. }
  41. public void setStudents(Set students)
  42. {
  43. this.students = students;
  44. }
  45. @Override
  46. public String toString()
  47. {
  48. return &quot;School [Name = &#39;&quot; + name + &quot;&#39;]&quot;;
  49. }
  50. }
  51. @Entity
  52. @Table(name = &quot;student&quot;)
  53. public class Student
  54. {
  55. @Id
  56. @SequenceGenerator(name = &quot;SequenceStudentId&quot;, sequenceName = &quot;Sequence_Student&quot;, allocationSize = 1)
  57. @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;SequenceStudentId&quot;)
  58. @Column(name = &quot;STUDENT_ID&quot;)
  59. private int id;
  60. @Column(name = &quot;NAME&quot;)
  61. private String name;
  62. @Column(name = &quot;AGE&quot;)
  63. private int age;
  64. @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  65. @JoinColumn(name=&quot;school_id&quot;, nullable=false)
  66. private School school;
  67. public Student()
  68. {}
  69. public Student(String name, int age, School school)
  70. {
  71. this.name = name;
  72. this.age = age;
  73. this.school = school;
  74. }
  75. public int getId()
  76. {
  77. return id;
  78. }
  79. public void setId(int id)
  80. {
  81. this.id = id;
  82. }
  83. public String getName()
  84. {
  85. return name;
  86. }
  87. public void setName(String name)
  88. {
  89. this.name = name;
  90. }
  91. public int getAge()
  92. {
  93. return age;
  94. }
  95. public void setAge(int age)
  96. {
  97. this.age = age;
  98. }
  99. @Override
  100. public String toString()
  101. {
  102. return &quot;Student [id=&quot; + id + &quot;, Name=&quot; + name + &quot;, Age=&quot; + age + &quot;]&quot;;
  103. }
  104. }

I get the following error on executing the application.

  1. org.hibernate.AnnotationException: @OneToOne or @ManyToOne on org.jfa.persistence.hibernate.entity.Student.school references an unknown entity: org.jfa.persistence.hibernate.entity.School

I have tried to tweak the relationship settings in the class in different ways as suggested by others on Internet/StackOverFlow. Yet, no luck so far. Please help.

答案1

得分: 0

你在 School.java 中错误地引用了 school_id,而不是 student_id。应该是:

  1. class School
  2. {
  3. ...
  4. @OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
  5. @JoinColumn(name="student_id", nullable=false)
  6. private Set<Student> students;
  7. ...
  8. }
英文:

You have wrongly referenced school_id instead of student_id in School.java. It should be:

  1. class School
  2. {
  3. ...
  4. @OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
  5. @JoinColumn(name=&quot;student_id&quot;, nullable=false)
  6. private Set&lt;Student&gt; students;
  7. ...
  8. }

huangapple
  • 本文由 发表于 2020年7月26日 18:10:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/63098741.html
匿名

发表评论

匿名网友

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

确定