java.lang.ExceptionInInitializerError 和 org.hibernate.AnnotationException: mappedBy 引用了一个未知的目标实体属性:

Posted

技术标签:

【中文标题】java.lang.ExceptionInInitializerError 和 org.hibernate.AnnotationException: mappedBy 引用了一个未知的目标实体属性:【英文标题】:java.lang.ExceptionInInitializerError and org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: 【发布时间】:2015-07-11 07:26:45 【问题描述】:

我使用 Hibernate 逆向工程来创建我的模型及其映射和关系。但是,当我尝试进行简单选择时,会显示错误。我不知道我的映射有什么问题。这是两个模型的代码。学生模型是父模型,评估模型是子模型。

评估模型:

            import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.AttributeOverride;
    import javax.persistence.AttributeOverrides;
    import javax.persistence.Column;
    import javax.persistence.EmbeddedId;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    /**
     * Assessment generated by hbm2java
     */
    @Entity
    @Table(name = "Assessment", catalog = "mydb")
    public class AssessmentModel implements java.io.Serializable 

        private AssessmentIdModel id;
        private SpEdTeacherModel spedteacher;
        private StudentModel Student;
        private Date assessmentDate;
        private Set<IepModel> ieps = new HashSet<IepModel>(0);
        private Set<SkillassessmentModel> skillassessments = new HashSet<SkillassessmentModel>(0);

        public AssessmentModel() 
        

        public AssessmentModel(AssessmentIdModel id, SpEdTeacherModel spedteacher, StudentModel Student, Date assessmentDate) 
            this.id = id;
            this.spedteacher = spedteacher;
            this.Student = Student;
            this.assessmentDate = assessmentDate;
        

        public AssessmentModel(AssessmentIdModel id, SpEdTeacherModel spedteacher, StudentModel Student, Date assessmentDate, Set<IepModel> ieps,
                Set<SkillassessmentModel> skillassessments) 
            this.id = id;
            this.spedteacher = spedteacher;
            this.Student = Student;
            this.assessmentDate = assessmentDate;
            this.ieps = ieps;
            this.skillassessments = skillassessments;
        

        @EmbeddedId

        @AttributeOverrides(
                @AttributeOverride(name = "assessmentId", column = @Column(name = "assessment_id", nullable = false) ),
                @AttributeOverride(name = "spedId", column = @Column(name = "sped_id", nullable = false) ),
                @AttributeOverride(name = "studentId", column = @Column(name = "student_id", nullable = false) ) )
        public AssessmentIdModel getId() 
            return this.id;
        

        public void setId(AssessmentIdModel id) 
            this.id = id;
        

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "sped_id", nullable = false, insertable = false, updatable = false)
        public SpEdTeacherModel getSpedteacher() 
            return this.spedteacher;
        

        public void setSpedteacher(SpEdTeacherModel spedteacher) 
            this.spedteacher = spedteacher;
        

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "student_id", nullable = false, insertable = false, updatable = false)
        public StudentModel getStudent() 
            return this.Student;
        

        public void setStudent(StudentModel Student) 
            this.Student = Student;
        

        @Temporal(TemporalType.DATE)
        @Column(name = "assessment_date", nullable = false, length = 10)
        public Date getAssessmentDate() 
            return this.assessmentDate;
        

        public void setAssessmentDate(Date assessmentDate) 
            this.assessmentDate = assessmentDate;
        

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "Assessment")
        public Set<IepModel> getIeps() 
            return this.ieps;
        

        public void setIeps(Set<IepModel> ieps) 
            this.ieps = ieps;
        

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "Assessment")
        public Set<SkillassessmentModel> getSkillassessments() 
            return this.skillassessments;
        

        public void setSkillassessments(Set<SkillassessmentModel> skillassessments) 
            this.skillassessments = skillassessments;
        

    

学生模型:

        import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import static javax.persistence.GenerationType.IDENTITY;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.ManyToOne;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    /**
     * Student generated by hbm2java
     */
    @Entity
    @Table(name = "Student", catalog = "mydb")
    public class StudentModel implements java.io.Serializable 

        private Integer studentId;
        private GuardianModel guardian;
        private String studentFname;
        private String studentMname;
        private String studentLname;
        private Date studentBirthdate;
        private int studentAge;
        private String studentGender;
        private String studentNationality;
        private String studentProgram;
        private Set<AssessmentModel> assessments = new HashSet<AssessmentModel>(0);
        private Set<StudenthastherapistModel> studenthastherapists = new HashSet<StudenthastherapistModel>(0);

        public StudentModel() 
        

        public StudentModel(GuardianModel guardian, String studentFname, String studentMname, String studentLname,
                Date studentBirthdate, int studentAge, String studentGender, String studentNationality, String studentProgram) 
            this.guardian = guardian;
            this.studentFname = studentFname;
            this.studentMname = studentMname;
            this.studentLname = studentLname;
            this.studentBirthdate = studentBirthdate;
            this.studentAge = studentAge;
            this.studentGender = studentGender;
            this.studentNationality = studentNationality;
            this.studentProgram = studentProgram;
        

        public StudentModel(GuardianModel guardian, String studentFname, String studentMname, String studentLname,
                Date studentBirthdate, int studentAge, String studentGender,
                String studentNationality, String studentProgram, Set<AssessmentModel> assessments,
                Set<StudenthastherapistModel> studenthastherapists) 
            this.guardian = guardian;
            this.studentFname = studentFname;
            this.studentMname = studentMname;
            this.studentLname = studentLname;
            this.studentBirthdate = studentBirthdate;
            this.studentAge = studentAge;
            this.studentGender = studentGender;
            this.studentNationality = studentNationality;
            this.studentProgram = studentProgram;
            this.assessments = assessments;
            this.studenthastherapists = studenthastherapists;
        

        @Id
        @GeneratedValue(strategy = IDENTITY)

        @Column(name = "student_id", unique = true, nullable = false)
        public Integer getStudentId() 
            return this.studentId;
        

        public void setStudentId(Integer studentId) 
            this.studentId = studentId;
        

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "guardian_id", nullable = false)
        public GuardianModel getGuardian() 
            return this.guardian;
        

        public void setGuardian(GuardianModel guardian) 
            this.guardian = guardian;
        

        @Column(name = "student_fname", nullable = false, length = 45)
        public String getStudentFname() 
            return this.studentFname;
        

        public void setStudentFname(String studentFname) 
            this.studentFname = studentFname;
        

        @Column(name = "student_mname", nullable = false, length = 45)
        public String getStudentMname() 
            return this.studentMname;
        

        public void setStudentMname(String studentMname) 
            this.studentMname = studentMname;
        

        @Column(name = "student_lname", nullable = false, length = 45)
        public String getStudentLname() 
            return this.studentLname;
        

        public void setStudentLname(String studentLname) 
            this.studentLname = studentLname;
        

        @Temporal(TemporalType.DATE)
        @Column(name = "student_birthdate", nullable = false, length = 10)
        public Date getStudentBirthdate() 
            return this.studentBirthdate;
        

        public void setStudentBirthdate(Date studentBirthdate) 
            this.studentBirthdate = studentBirthdate;
        

        @Column(name = "student_age", nullable = false)
        public int getStudentAge() 
            return this.studentAge;
        

        public void setStudentAge(int studentAge) 
            this.studentAge = studentAge;
        

        @Column(name = "student_gender", nullable = false, length = 6)
        public String getStudentGender() 
            return this.studentGender;
        

        public void setStudentGender(String studentGender) 
            this.studentGender = studentGender;
        

        @Column(name = "student_nationality", nullable = false, length = 15)
        public String getStudentNationality() 
            return this.studentNationality;
        

        public void setStudentNationality(String studentNationality) 
            this.studentNationality = studentNationality;
        

        @Column(name = "student_program", nullable = false, length = 45)
        public String getStudentProgram() 
            return this.studentProgram;
        

        public void setStudentProgram(String studentProgram) 
            this.studentProgram = studentProgram;
        

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "Student")
        public Set<AssessmentModel> getAssessments() 
            return this.assessments;
        

        public void setAssessments(Set<AssessmentModel> assessments) 
            this.assessments = assessments;
        

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "Student")
        public Set<StudenthastherapistModel> getStudenthastherapists() 
            return this.studenthastherapists;
        

        public void setStudenthastherapists(Set<StudenthastherapistModel> studenthastherapists) 
            this.studenthastherapists = studenthastherapists;
        

    

这是错误:

            HTTP Status 500 - javax.servlet.ServletException: java.lang.ExceptionInInitializerError
    type Exception report
    message javax.servlet.ServletException:   java.lang.ExceptionInInitializerError
    description The server encountered an internal error that prevented it from fulfilling this request.
    exception 
    org.apache.jasper.JasperException: javax.servlet.ServletException:  java.lang.ExceptionInInitializerError
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:556)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:462)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    root cause 
    javax.servlet.ServletException: java.lang.ExceptionInInitializerError
    org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:916)
    org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:845)
        org.apache.jsp.index_jsp._jspService(index_jsp.java:138)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


    root cause 
    java.lang.ExceptionInInitializerError
    softdev.asese.utils.HibernateUtil.<clinit>(HibernateUtil.java:17)
    softdev.asese.dao.GuardianDao.getGuardian(GuardianDao.java:16)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:93)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)



    root cause 
    org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: softdev.asese.models.AssessmentModel.Student in softdev.asese.models.StudentModel.assessments
        org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:769)
        org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:729)
        org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70)
        org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1697)
        org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1426)
        org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
        org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
        softdev.asese.utils.HibernateUtil.<clinit>(HibernateUtil.java:14)
        softdev.asese.dao.GuardianDao.getGuardian(GuardianDao.java:16)
        org.apache.jsp.index_jsp._jspService(index_jsp.java:93)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

【问题讨论】:

【参考方案1】:

您的课程似乎没问题,除了 AssessmentModel 中您的属性 Student 的大写。尝试将属性名称和 mappedBy 引用更改为小写。此外,您可以尝试向注释中添加一些参数,例如我的示例。我对 hibernate 比较陌生,知道注释中的一个简单单词可能很重要。

在我的父类中,我有...

@OneToMany(fetch=FetchType.LAZY, mappedBy="cobpresGestion")
@Cascade(CascadeType.ALL)
public Set<CobpresOptionDet> getCobpresOptionDets() 
    return this.cobpresOptionDets;

在我的孩子班我有......

@ManyToOne(fetch=FetchType.LAZY, optional=false)
@JoinColumn(name="IDCOBPRES", nullable=false, insertable=true, updatable=true)
public CobpresGestion getCobpresGestion() 
    return this.cobpresGestion;

【讨论】:

谢谢!我已经更改了名称,现在可以使用了!【参考方案2】:

异常告诉你出了什么问题:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "Student")

mappedBy 引用了一个未知的目标实体属性:softdev.asese.models.AssessmentModel.Student in softdev.asese.models.StudentModel.assessments

应该是

@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")

【讨论】:

谢谢!我已经更改了名称,现在可以使用了!

以上是关于java.lang.ExceptionInInitializerError 和 org.hibernate.AnnotationException: mappedBy 引用了一个未知的目标实体属性:的主要内容,如果未能解决你的问题,请参考以下文章