当前位置: 代码迷 >> Web前端 >> Hibernate的org.hibernate.MappingException: Unknown entity
  详细解决方案

Hibernate的org.hibernate.MappingException: Unknown entity

热度:974   发布时间:2013-08-06 16:47:25.0
Hibernate的org.hibernate.MappingException: Unknown entity:
许多初学者在学习Hibernate的时候,总会遇到这样的问题。org.hibernate.MappingException: Unknown entity: ******
今天我在调试程序的时候也遇到了此问题,最终终于解决,下面我的一些总结下。
一句话:“因为你的映射文件(*.hbm.xml) 没有被映射”
问题出现的原因,有几下几点:
1,检查你的映射文件的名字是否和你的pojo(*.java)的名字是否相同。
2.映射文件的名字是*.hbm.xml而不是*.xml
3.你是否加载了你的映射文件。
      加载的方法有两种
   (1)在你的Hibernate.cfg.xml配置文件中加载映射文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>
              …………
              …………
         <!-- 用于记载映射文件Student.hbm.xml-->
     <mapping resource="com/Students.hbm.xml"/>
     </session-factory>
</hibernate-configuration>
    (2)在你的测试代码的加载映射文件
           Configuration cfg = new Configuration();
           cfg.configure();
           cfg.addClass(*.class);    这里的*.class是你的映射文件的名字*.hbm.class中的*。。。
      但是你要注意,用第二种方法加载的时候,你的*.hbm.class文件必须位于classpath下面。
  相关解决方案