【问题标题】:Hibernate HBM Mapping ProblemHibernate HBM 映射问题
【发布时间】:2011-03-25 21:23:18
【问题描述】:

我有以下三个类:

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 文件:

学校.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>

学生.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 映射做错了什么吗?谢谢!

【问题讨论】:

    标签: java hibernate hibernate-mapping hbm


    【解决方案1】:

    您应该在 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>
    

    【讨论】:

    • 好吧,在我的示例中,没有完全限定的名称。 Student、StudentSchool 和 School 类在默认包中。
    猜你喜欢
    • 1970-01-01
    • 2017-01-09
    • 1970-01-01
    • 2011-07-31
    • 2021-10-24
    • 1970-01-01
    • 2014-01-31
    • 2016-08-07
    • 2011-01-18
    相关资源
    最近更新 更多