我已经阅读了许多视频和教程,解释了如何使用注释机制在hibernate中配置一对多关系。我仍然收到这个错误。
错误是: org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany定位未映射的类:bean.Professor.coursesAssigned [bean.Course] 使用@OneToMany或@ManyToMany定位未映射的类:bean.Professor.coursesAssigned [bean.Course]
我的课程是:
Professor.java
package bean;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
@Entity
public class Professor extends User{
@OneToMany(targetEntity = Course.class, mappedBy = "assignedProfessor",
cascade = CascadeType.ALL , fetch = FetchType.LAZY)
private Set<Course> coursesAssigned;
}
和course.java是:
package bean;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
public class Course {
private Integer courseId;
private String courseName;
@ManyToOne(targetEntity = Professor.class)
@JoinColumn(name = "professor_join")
private Professor assignedProfessor;
}
答案 0 :(得分:5)
org.hibernate.AnnotationException:
Use of @OneToMany or @ManyToMany targeting an unmapped class: bean.Professor.coursesAssigned[bean.Course]
Use of @OneToMany or @ManyToMany targeting an unmapped class: bean.Professor.coursesAssigned[bean.Course]
该例外已经解释了原因。 @OneToMany
和@ManyToMany
只能在其类为映射类的属性上进行注释。如果一个类使用@Entity
进行注释,并且在<mapping class>
实例中包含为programmatically included in the Configuration,则该类被视为已映射。
所以,我相信在@Entity
课上标记Course
后,该异常将会消失。