【问题标题】:Hibernate configuration error - one to many relation using annotationHibernate 配置错误 - 使用注释的一对多关系
【发布时间】:2012-04-12 03:24:39
【问题描述】:

我浏览了许多视频和教程,解释了如何使用注释机制在休眠中配置一对多关系。我仍然收到此错误。

错误是: org.hibernate.AnnotationException:使用@OneToMany 或@ManyToMany 定位未映射的类:bean.Professor.coursesAssigned[bean.Course] 使用@OneToMany 或@ManyToMany 定位未映射的类:bean.Professor.coursesAssigned[bean.Course]

我的课是:

教授.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;
    }

【问题讨论】:

    标签: hibernate hibernate-mapping hibernate-onetomany


    【解决方案1】:
    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 注释并包含在配置文件中的 &lt;mapping class&gt;programmatically included in the Configuration 实例中,则该类被视为已映射。

    所以,我相信在Course 类上标记@Entity 后异常会消失。

    【讨论】:

    • 非常感谢您的帮助.. 真的很管用.. 它只是从我的脑海中溜走。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    相关资源
    最近更新 更多