Question

I have gone through many videos and tutorials explaining about how to configure one to many relation in hibernate using annotation mechanism. Still I am getting this error.

the error is : 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]

My classes are :

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;
    }

and course.java is :

    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;
    }
Was it helpful?

Solution

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]

The exception has already explained the causes . @OneToMany and @ManyToMany can only be annotated on the property whose class is a mapped class. A class is considered as mapped if they are annotated with @Entity and is included as <mapping class> in the configuration file or programmatically included in the Configuration instance.

So , I believe the exception will be gone after you mark @Entity on the class Course.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top