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
如果我取消注释到“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在 hbm 文件中提供类的完全限定名称。
像 somepackage.StudentSchool
编辑:如果所有都在同一个包中,则尝试添加此
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