Hibernate HBM映射问题

时间:2011-03-25 21:23:18

标签: java hibernate hibernate-mapping hbm

我有以下三个类:

public class Student {
    private Integer studentId;
    private StudentSchool studentSchool;
    private School school;

    public Integer getStudentId() {
        return studentId;
    }
    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }
    public StudentSchool getStudentSchool() {
        return studentSchool;
    }
    public School getSchool() {
        return school;
    }
    public void setSchool(School school) {
        this.school = school;
    }
}

public class StudentSchool {
    private Student student;
    private School school;  

    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public School getSchool() {
        return school;
    }
    public void setSchool(School school) {
        this.school = school;
    }
}

public class School {
    private Integer schoolId;
    private Set allStudents;

    public Integer getSchoolId() {
        return schoolId;
    }
    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }
    public Set getAllStudents() {
        return allStudents;
    }
    public void setAllStudents(Set allStudents) {
        this.allStudents = allStudents;
    }
}

我有以下DDL:

create table Student (StudentId Integer);
create table StudentSchool (SchoolId Integer, StudentId Integer);
create table School (SchoolId Integer Primary Key);

我有以下HBM文件:

School.hbm.xml

<hibernate-mapping>
    <class name="School" table="School">
        <property name="schoolId" type="integer" />
        <set name="allStudents" table="StudentSchool" fetch="join">
            <key column="schoolId" />
            <composite-element class="StudentSchool">
                <parent name="school"/>
                <many-to-one name="student" column="studentId" not-null="true" class="Student" />
            </composite-element>
        </set>
    </class>
</hibernate-mapping>

Student.hbm.xml

<hibernate-mapping>
    <class name="Student" table="Student">
        <property name="studentId" type="integer" />
        <one-to-one name="studentSchool" class="StudentSchool" />
        <!-- <one-to-one name="school" class="School" /> -->
    </class>
</hibernate-mapping>

当我尝试根据学校查询Student对象时,我得到以下异常(即使我在类路径中编译了一个名为“StudentSchool”的类):

org.hibernate.MappingException: persistent class not known: StudentSchool

如果我将“一对一”映射取消注释为“School”并将一对一映射注释为“studentSchool”,则会出现以下异常:

<AST>:1:143: unexpected AST node: : java.lang.NullPointerException

有没有人对我的hbm制图错误做了什么想法?谢谢!

1 个答案:

答案 0 :(得分:3)

您应该在hbm文件中提供类的完全限定名称。

喜欢somepackage.StudentSchool

编辑:如果所有人都在同一个套餐中,请尝试添加此

<hibernate-mapping>
    <class name="Student" table="Student">
        <property name="studentId" type="integer" />
        <one-to-one name="studentSchool" class="StudentSchool" property-ref="student"/>
        <!-- <one-to-one name="school" class="School" property-ref="student"/> -->
    </class>
</hibernate-mapping>