java - Hibernate HBM 映射问题

标签 java hibernate hibernate-mapping hbm

我有以下三个类(class):

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 映射出了什么问题吗?谢谢!

最佳答案

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

关于java - Hibernate HBM 映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5438277/

相关文章:

java - OSMdroid 找不到类 GeoPoint

java - 如何使用 Hibernate Context session 创建通用 DAO 类

Hibernate 抛出 org.hibernate.persister.entity.SingleTableEntityPersister

java - 如果我在 Hibernate 中使用 JPA,我的项目应该有哪些依赖项?

java - 如何注释 map 以连接两个看似不相关的类?

java - 通过 Hibernate 注解创建外键关系的问题

java - 如何将 gecko 可执行文件与 Selenium 一起使用

java - configuration.yml 出现错误 : * Unrecognized field at: driverClass - DROPWIZARD

c# - 使用 java 和 WP7 进行 AES 加密

hibernate-mapping - 休眠 : Adding a new element to association list does not persist