org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Ques#tion.examId references an unknown entity: long
If you look closely at your stacktrace, you will see
Caused by: org.hibernate.AnnotationException: @OneToOne or @ManyToOne on entities.Question.examId references an unknown entity: long
So this field
@ManyToOne
private long examId;
is causing the problem. @ManyToOne
has a paramater targetEntity
which says:
(Optional) The entity class that is the target of the association. Defaults to the type of the field or property that stores the association.
Since you haven't provided that parameter, it defaults to long
, which is not a managed Entity
.
You'll probably want to use
@ManyToOne(targetEntity = Exam.class)
private long examId;
otherwise it won't know what to map to. Or even better
@ManyToOne
private Exam exam;
Just add the class Team to the "hibernate-cfg.xml" file, because Hibernate doesn't identify without adding into it.
FYI, this sometimes happens if you have a hibernate annotation:
@org.hibernate.annotations.Entity
and a JPA annotation:
@javax.persistence.Entity
mixed up
Edit:
Explicitly import the javax annotation.