错误/异常:org.hibernate.MappingException: Unknown entity: com.shore.entity.Student 的解决方法
1、错误/异常图
错误/异常描述:Hibernate配置文件 映射异常,不明实体类Student(org.hibernate.MappingException: Unknown entity: com.shore.entity.Student)
我原先的Hibernate配置文件:hibernate.cfg.xml
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <!-- Database connection settings --> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="connection.url">jdbc:mysql://localhost:3306/school</property> 11 <property name="connection.username">root</property> 12 <property name="connection.password">root</property> 13 14 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 15 <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 16 <property name="show_sql">true</property> 17 <property name="hbm2ddl.auto">create</property> 18 19 <!-- <mapping resource="com/shore/entity/Student.hbm.xml"/> --> 20 </session-factory> 21 </hibernate-configuration>
因为我用的是注解的方式,故没有xxx.hbm.xml配置文件,所以上面我注释掉了<!-- <mapping resource="com/shore/entity/Student.hbm.xml"/> --> ,却忘记引入Student实体类。
2、解决方法
在Hibernate的配置文件里加上一句:<mapping class="com.shore.entity.Student" /> 就解决了。
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <!-- Database connection settings --> 9 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 10 <property name="connection.url">jdbc:mysql://localhost:3306/school</property> 11 <property name="connection.username">root</property> 12 <property name="connection.password">root</property> 13 14 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 15 <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 16 <property name="show_sql">true</property> 17 <property name="hbm2ddl.auto">create</property> 18 19 <!-- <mapping resource="com/shore/entity/Student.hbm.xml"/> --> 20 <mapping class="com.shore.entity.Student" /> 21 </session-factory> 22 </hibernate-configuration>
原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/11528837.html 版权声明:欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!) |