2012-04-12 95 views
0

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

回答

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进行注释并且在配置文件或programmatically included in the Configuration实例中包含为<mapping class>,则认为该类是映射的。

所以,我相信在你对Course这个类标记@Entity之后,异常将会消失。

+0

非常感谢您的帮助..这真的有效..它只是滑出我的脑海。 – user1324493 2012-04-12 05:53:56