Unknown entity (Hibernate的findById方法参数必须加上包名)

使用Hibernate的findById(java.lang.Integer id)方法

Code:
  1. public Requestnote findById(java.lang.Integer id) { 
  2.     try {  
  3.         Requestnote instance = (Requestnote) getSession().get(  
  4.                 "Requestnote", id);  
  5.         return instance;  
  6.     } catch (RuntimeException re) {  
  7.         log.error("get failed", re);  
  8.         throw re;  
  9.     }  
  10. }  

 

报异常:

org.hibernate.MappingException: Unknown entity:Requestnote

网上百度,结果是因为findById()方法中实体类参数要带上包名。Requestnote要带上包名com.XXX.XXX写成com.XXX.XXX.Requestnote 就好了。

Code:
  1. public Requestnote findById(java.lang.Integer id) {  
  2.     try {  
  3.         Requestnote instance = (Requestnote) getSession().get(  
  4.                 "com.XXX.XXX.Requestnote", id);  
  5.         return instance;  
  6.     } catch (RuntimeException re) {  
  7.         log.error("get failed", re);  
  8.         throw re;  
  9.     }  
  10. }  

下面是我参考的网站:

http://tieba.baidu.com/f?kz=545576481

http://hi.baidu.com/five824/blog/item/02664cd02171dfdf562c842c.html

________________________________________________________________________________

 

通过ProductDAO 查询时出现异常 
Exception in thread "main" org.hibernate.MappingException: Unknown entity: Product 
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514) 
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:66) 
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:862) 
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:799) 
at com.ydq.DAO.ProductDAO.findById(ProductDAO.java:65) 
at com.ydq.test.Test.main(Test.java:10) 

进过测试用productDAO.findByName("pen"); 没有错误 
找了半天发现是ProductDAO里的findById 方法有问题.有问题的代码是:try { 
            Product instance = (Product) getSession() 
                    .get("Product", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
因该改为 
try { 
            Product instance = (Product) getSession() 
                    .get("com.ydq.model.Product", id); 
            return instance; 
        } catch (RuntimeException re) { 
            log.error("get failed", re); 
            throw re; 
        } 
就是参数必须加上包名,其他方法不需要加,findById却要加

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值