2011-03-25 131 views
2

我有以下三類:Hibernate的HBM映射問題

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 

如果我取消了一個一對一映射,以「學校」和註釋掉一個一對一映射,以「studentSchool」,我得到以下異常:

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

沒有人有任何我我對我的hbm映射做錯了什麼?謝謝!

回答

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> 
+0

嗯,在我的例子中,沒有完全合格的名稱。學生,學生學校和學校課程都在默認包中。 – David 2011-03-28 13:02:17