2011-03-25 3 views
2

내가 가지고있는 다음과 같은 세 가지 클래스 :최대 절전 모드 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> 

내가 학교에 따라 학생 객체를 조회하려고 , (나는 "StudentSchool"라는 클래스 내 클래스 경로에 컴파일 된 경우에도) 나는 다음과 같은 예외가 :

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

사람이 있습니까 : 나는 일대일 맵핑을 "학교"의 주석을 해제하고 "studentSchool는"나는 다음과 같은 예외를 얻을 수있는 일대일 맵핑을 주석 경우

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

어떤 내가 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

음, 내 예에서, 더 완전한 이름이없는 추가하려고합니다. Student, StudentSchool 및 School 클래스는 기본 패키지에 있습니다. – David