Hibernate配置错误 - 使用注释的一对多关系

时间:2012-04-12 03:24:39

标签: hibernate hibernate-mapping hibernate-onetomany

我已经阅读了许多视频和教程,解释了如何使用注释机制在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;
    }

1 个答案:

答案 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后,该异常将会消失。