2

次の3つのクラスがあります。

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" への 1 対 1 マッピングのコメントを外し、"studentSchool" への 1 対 1 マッピングをコメント アウトすると、次の例外が発生します。

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

hbm マッピングで私が間違っていたことについて何か考えがある人はいますか? ありがとう!

4

1 に答える 1

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>
于 2011-03-25T21:59:39.427 に答える