首页 > 代码库 > Mingyang.net:注解配置Hibernate时报错Unknown Entity

Mingyang.net:注解配置Hibernate时报错Unknown Entity

注解配置时报错:org.hibernate.MappingException: Unknown entity: net.mingyang.cms.bean.User

org.hibernate.MappingException: Unknown entity: net.mingyang.cms.bean.User    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096)    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443)    at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:242)    at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:511)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:100)    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:684)    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:676)    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:671)    at net.mingyang.cms.dao.hibernate.UserDaoHibernate.updateUser(UserDaoHibernate.java:34)    at net.mingyang.cms.service.impl.UserServiceImpl.updateUser(UserServiceImpl.java:50)    at net.mingyang.cms.controller.UserController.indexAction(UserController.java:61)    . . . . . .

可能的原因:
1、配置实体类时为@Entity导包时错误的导入了 rg.hibernate.annotations.Entity ,应该更改为import javax.persistence.Entity;
2、applicationContext.xml中配置实体扫描时路径出错, 比如:实体类存放在net.mingyang.cms.bean包下,则路径就要配为

<property name="packagesToScan" value="net.mingyang.cms.*"/>

而不是 

<property name="packagesToScan" value="net.mingyang.cms.bean.*"/>

也就是说配置映射目录要比实际少一层。 
或者直接使用 

<property name="annotatedClasses">    <list>        <value>net.mingyang.cms.bean.User</value>        ......    </list></property> 

指定相应的实体类(实体类较少时)。