控制台出现的主要错误信息:Caused by: org.hibernate.MappingException: class cn.fpxty.LinkMan not found while looking for property: linkid
错误信息:
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at com.v512.examples.HibernateTest.main(HibernateTest.java:23)
Caused by: org.hibernate.MappingException: cn.fpxty.entity.LinkMan not found while looking for property: linkid
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:232)
at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:302)
at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:423)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:356)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:295)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:166)
at org.hibernate.cfg.Configuration.add(Configuration.java:716)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:551)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
... 7 more
Caused by: java.lang.ClassNotFoundException: cn.fpxty.LinkMan
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:228)
... 15 more
实体类配置文件如下
LinkMan.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.fpxty.entity.LinkMan" table="t_linkman">
<id name="linkid" column="linkid">
<!-- 设置数据库中表的id增长的策略,native:自动增长 -->
<generator class="native"></generator>
</id>
<!-- 配置其他属性和表字段的对应 -->
<property name="lkmName" column="lkmName"></property>
<property name="lkmGender" column="lkmGender"></property>
<property name="lkmPhone" column="lkmPhone"></property>
<property name="lkmMobile" column="lkmMobile"></property>
</class>
</hibernate-mapping>
解决方案:
实体类映射配置文件中,在配置其他属性和表的字段对应是应加上数据类型。
更改后的映射配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.fpxty.entity.LinkMan" table="t_linkman">
<id name="linkid" column="linkid">
<!-- 设置数据库中表的id增长的策略,native:自动增长 -->
<generator class="native"></generator>
</id>
<!-- 配置其他属性和表字段的对应 -->
<property name="lkmName" column="lkmName" type="java.lang.String"></property>
<property name="lkmGender" column="lkmGender" type="java.lang.String"></property>
<property name="lkmPhone" column="lkmPhone" type="java.lang.String"></property>
<property name="lkmMobile" column="lkmMobile" type="java>lang.String"></property>
</class>
</hibernate-mapping>
总结:
此方法可以完美解决这个问题,但是,当我使用过这种方法后,运行一次后,在还原回原来不写数据类型的时候,再次运行则不会出现此类错误,亲测实用!