英文:
Constantly getting org.hibernate.AnnotationException: @OneToOne or @ManyToOne references an unknown entity: error
问题
我可以成功地通过Db2控制台将记录插入到表中。
INSERT INTO db2admin.school VALUES (1, 'BJS');
INSERT INTO db2admin.STUDENT VALUES (1, 'Pankaj', 30, 1);
我有以下的表。
CREATE TABLE DB2ADMIN.School (school_id SMALLINT PRIMARY KEY NOT NULL, name varchar(128));
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中如何定义实体关系。
@Entity
@Table(name = "school")
public class School {
@Id
@SequenceGenerator(name = "SchoolIdSequence", schema = "db2admin", sequenceName = "Sequence_School", allocationSize = 1)
@GeneratedValue(generator = "SchoolIdSequence", strategy = GenerationType.SEQUENCE)
@Column(name = "SCHOOL_ID")
private int id;
@Column(name = "NAME")
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "school")
//@JoinColumn(name = "student_id", nullable = false)
private Set<Student> students;
public School() {}
public School(String name) {
this.name = name;
}
// ... 省略其他方法和属性的定义
}
@Entity
@Table(name = "student")
public class Student {
@Id
@SequenceGenerator(name = "SequenceStudentId", sequenceName = "Sequence_Student", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceStudentId")
@Column(name = "STUDENT_ID")
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private int age;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "school_id", nullable = false)
private School school;
public Student() {}
public Student(String name, int age, School school) {
this.name = name;
this.age = age;
this.school = school;
}
// ... 省略其他方法和属性的定义
}
在执行应用程序时,我收到以下错误。
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.
INSERT INTO db2admin.school VALUES (1, 'BJS');
INSERT INTO db2admin.STUDENT VALUES (1, 'Pankaj', 30, 1);
I have the following tables.
CREATE TABLE DB2ADMIN.School (school_id SMALLINT PRIMARY KEY NOT NULL, name varchar(128));
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.
@Entity
@Table(name = "school")
public class School
{
@Id
@SequenceGenerator(name = "SchoolIdSequence", schema="db2admin", sequenceName = "Sequence_School", allocationSize = 1)
@GeneratedValue(generator = "SchoolIdSequence", strategy = GenerationType.SEQUENCE)
@Column(name = "SCHOOL_ID")
private int id;
@Column(name = "NAME")
private String name;
@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy = "school")
//@JoinColumn(name="student_id", nullable=false)
private Set<Student> students;
public School()
{}
public School(String name)
{
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Set getStudents()
{
return students;
}
public void setStudents(Set students)
{
this.students = students;
}
@Override
public String toString()
{
return "School [Name = '" + name + "']";
}
}
@Entity
@Table(name = "student")
public class Student
{
@Id
@SequenceGenerator(name = "SequenceStudentId", sequenceName = "Sequence_Student", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceStudentId")
@Column(name = "STUDENT_ID")
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "AGE")
private int age;
@ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name="school_id", nullable=false)
private School school;
public Student()
{}
public Student(String name, int age, School school)
{
this.name = name;
this.age = age;
this.school = school;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public String toString()
{
return "Student [id=" + id + ", Name=" + name + ", Age=" + age + "]";
}
}
I get the following error on executing the application.
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。应该是:
class School
{
...
@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="student_id", nullable=false)
private Set<Student> students;
...
}
英文:
You have wrongly referenced school_id instead of student_id in School.java. It should be:
class School
{
...
@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="student_id", nullable=false)
private Set<Student> students;
...
}
专注分享java语言的经验与见解,让所有开发者获益!
评论