首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Hibernate HBM映射问题

Hibernate HBM映射问题
EN

Stack Overflow用户
提问于 2011-03-26 05:23:18
回答 1查看 4.8K关注 0票数 2

我有以下三个类:

代码语言:javascript
复制
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:

代码语言:javascript
复制
create table Student (StudentId Integer);
create table StudentSchool (SchoolId Integer, StudentId Integer);
create table School (SchoolId Integer Primary Key);

我有以下HBM文件:

School.hbm.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

当我尝试基于学校查询学生对象时,我得到了以下异常(尽管我的类路径中编译了一个名为"StudentSchool“的类):

代码语言:javascript
复制
org.hibernate.MappingException: persistent class not known: StudentSchool

如果我取消注释到“学校”的一对一映射,并注释掉到"studentSchool“的一对一映射,我会得到以下异常:

代码语言:javascript
复制
<AST>:1:143: unexpected AST node: : java.lang.NullPointerException

有谁知道我的hbm映射出了什么问题吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-26 05:59:39

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

像somepackage.StudentSchool一样

编辑:如果所有包都在同一个包中,那么尝试添加以下内容

代码语言:javascript
复制
<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>
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5438277

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档