问题:
使用annotation配置SSH2项目时,遇到这样的错误:
org.hibernate.MappingException: Unknown entity: com.jp.bean.TUser
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:628)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:843)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:836)
at com.jp.dao.UserDao.getTUser(UserDao.java:31)
......
提示未知的实体。 可能的原因如下:
1、配置实体类时为@Entity导包时 错误的导入了 import org.hibernate.annotations.Entity 应该更改为import javax.persistence.Entity;
错误的:
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.Entity;//这是就出问题了哦
@Entity
@Table(name="test_person")
public class Person implements Serializable
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.Entity;//引入这个类就正确了
@Entity
@Table(name="test_person")
public class Person implements Serializable
2、 可能是没有在cfg文件中加入 配置造成的。
3、applicationContext.xml中配置实体扫描时路径出错。比如:实体类存放在com.demo.entity包下,
错误的
<property name="packagesToScan"value="com.demo.entity.*"/>
正确的
<property name="packagesToScan"value="com.demo.entity"/>
本文引用以下两篇文章:
http://www.blogjava.net/xiaochao/articles/hibernatetopic.html
http://www.thinksaas.cn/group/topic/105980/