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

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

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, &#39;BJS&#39;);

INSERT INTO db2admin.STUDENT VALUES (1, &#39;Pankaj&#39;, 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 = &quot;school&quot;)
public	class	School
{
    @Id
    @SequenceGenerator(name = &quot;SchoolIdSequence&quot;, schema=&quot;db2admin&quot;, sequenceName = &quot;Sequence_School&quot;, allocationSize = 1)
    @GeneratedValue(generator = &quot;SchoolIdSequence&quot;, strategy = GenerationType.SEQUENCE)
    @Column(name = &quot;SCHOOL_ID&quot;)
    private	int id;

    @Column(name = &quot;NAME&quot;)
    private String name;

	@OneToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL, mappedBy = &quot;school&quot;)
	//@JoinColumn(name=&quot;student_id&quot;, nullable=false)
	private Set&lt;Student&gt; 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 &quot;School [Name = &#39;&quot; + name + &quot;&#39;]&quot;;
    }
}

@Entity
@Table(name = &quot;student&quot;)
public	class	Student
{
    @Id
    @SequenceGenerator(name = &quot;SequenceStudentId&quot;, sequenceName  = &quot;Sequence_Student&quot;, allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = &quot;SequenceStudentId&quot;)
    @Column(name = &quot;STUDENT_ID&quot;)
    private int id;

    @Column(name = &quot;NAME&quot;)
    private String name;

    @Column(name = &quot;AGE&quot;)
    private int age;

    @ManyToOne(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name=&quot;school_id&quot;, 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 &quot;Student [id=&quot; + id + &quot;, Name=&quot; + name + &quot;, Age=&quot; + age + &quot;]&quot;;
    }
}

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=&quot;student_id&quot;, nullable=false)
    private Set&lt;Student&gt; students;

   ...
}

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:

确定