使用Hibernate的findById(java.lang.Integer id)方法
- public Requestnote findById(java.lang.Integer id) {
- try {
- Requestnote instance = (Requestnote) getSession().get(
- "Requestnote", id);
- return instance;
- } catch (RuntimeException re) {
- log.error("get failed", re);
- throw re;
- }
- }
报异常:
org.hibernate.MappingException: Unknown entity:Requestnote
网上百度,结果是因为findById()方法中实体类参数要带上包名。Requestnote要带上包名com.XXX.XXX写成com.XXX.XXX.Requestnote 就好了。
- public Requestnote findById(java.lang.Integer id) {
- try {
- Requestnote instance = (Requestnote) getSession().get(
- "com.XXX.XXX.Requestnote", id);
- return instance;
- } catch (RuntimeException re) {
- log.error("get failed", re);
- throw re;
- }
- }
下面是我参考的网站:
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却要加,还没弄清楚为什么?