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

时间:2015-07-11 07:26:46

标签: java hibernate hibernate-onetomany

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

评估模型:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String line = br.readLine();
int noOfStudents = Integer.parseInt(line); //firstline contains 1 integer.

ArrayList <Integer> marks = new ArrayList<Integer>();

line = br.readLine(); //second line contains a line of integers

StringTokenizer tokens = new StringTokenizer(line, "\\s+");
// to break the line into tokens 

while (tokens.hasMoreTokens())
{
    marks.add(Integer.valueOf(tokens.nextToken())); //error here
    //when I am converting tokens into integers its giving error
}

学生模特:

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

    }

2 个答案:

答案 0 :(得分:0)

例外情况告诉你错误:

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

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

应该是

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

答案 1 :(得分:0)

您的课程似乎没问题,除了您的属性Student在AssessmentModel中的大写字母。尝试将属性名称和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;
}