注解配置时报错:org.hibernate.MappingException: Unknown entity:

问题:

使用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/


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值