Hibernate HBM 映射问题

发布于 2024-10-26 21:33:30 字数 2926 浏览 5 评论 0原文

我有以下三个课程:

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 映射做错了什么有任何想法吗?谢谢!

I have the following three classes:

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

I have the following DDL:

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

I have the following HBM files:

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>

When I try to query the Student object based on the school, I get the following exception (even though I have a class called "StudentSchool" compiled in my classpath):

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

If I uncomment the one-to-one mapping to "School" and comment out the one-to-one mapping to "studentSchool", I get the following exception:

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

Does anyone have any ideas about what I did wrong with my hbm mapping? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2024-11-02 21:33:30

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

You should give the fully qualified name of your classes in the hbm files.

like somepackage.StudentSchool

EDIT: If all are in the same package, then try adding this

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文