org.hibernate.MappingException: Unknown entity
在调用findById方法时常出现的异常,有三个地方是需要注意的
1.确认hbm.xml文件中类路径要写全
===========================================
<hibernate-mapping>
<class catalog="shop" name="com.shop.entity.User" table="user">
<id name="rowId" type="java.lang.String">
<column length="32" name="RowID"/>
<generator class="uuid.hex"/>
</id>
<property generated="never" lazy="false" name="loginName" type="java.lang.String">
<column length="20" name="LoginName"/>
</property>
</class>
</hibernate-mapping>
============================================
2.applicationContext.xml(没有使用Spring框架的就找Hibernate的配置文件)中,hml.xml文件的路径是否正确
============================================
<property name="mappingResources">
<list>
<value>com/shop/entity/User.hbm.xml</value></list>
</property>
=============================================
3.很容易漏掉的地方,就是Session中的get方法
=============================================
public User findById( java.lang.String id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) getHibernateTemplate()
.get("com.shop.entity.User", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
==============================================
注意,这里User.java和User.hbm.xml我都是放在com.shop.entity路径下了,如果你不是放在同一个文件夹下,就按实际的路径写
Unknown entity findById
最新推荐文章于 2022-01-25 17:32:59 发布