Caused by:
org.hibernate.MappingException: Association references unmapped class: SubjectChapter(SubjectChapter为出错的类)
org.hibernate.MappingException: Association references unmapped class: SubjectChapter(SubjectChapter为出错的类)
从错误提示信息可知是有一个POJO对象(这里是SubjectChapter)没有映射到。但是发现自己已经写了相关的xml映射文件。
这时可以查看Hibernate.cfg.xml文件里有没有加入这个POJO的映射文件:
<mapping resource="com/exam/entity/QuestionType.hbm.xml"/>
或者查看该POJO对象的映射文件有没有写错,包括类路径和基本的属性映射。如果没有错再查看关联到了该POJO对象的映射文件中,该POJO对象的类路径有没有写错。
如下面的例子引起了该错误:
<hibernate-mapping> <class name="com.exam.entity.Subject" table="exam_subject"> <id name="subjectName" column="subject_name"></id> <many-to-one name="subjectType" column="subject_type_id" cascade="all"/> <set name="subjectChapter" inverse="true"> <key column="subject_id"/> <one-to-many class="SubjectChapter"/> </set> </class> </hibernate-mapping>
这里发现原来是set标签里的one-to-many的类路径没有写完整,修改添加完整的类路径:
<one-to-many class="com.exam.entity.SubjectChapter"/>
重启,问题解决了。
还有提点需要说明的是many-to-one并不需要写类路径,如上例,因为在POJO对象中都已具体给出了该类的路径:
package com.exam.entity; … private SubjectType subjectType;