使用eclipse自动生成的DAO文件时,调用dao方法出现以下错误,查资料得到需在实体类前加上包名
错误提示:
Hibernate: select user0_.USER_ID as USER1_1_0_, user0_.EMAIL as EMAIL1_0_, user0_.PASSWORD as PASSWORD1_0_, user0_.SEX as SEX1_0_, user0_.USER_NAME as USER5_1_0_ from bysj.user user0_ where user0_.USER_ID=?
[ERROR:23:07:18:]detail:find by property name failed(UserDAO.java:125)-[线程:http-nio-8080-exec-6]
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [from User as model where model.sex= ?]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:324)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3270)
UserDAO.java:125
public List findByProperty(String propertyName, Object value) {
log.debug("finding User instance with property: " + propertyName
+ ", value: " + value);
try {
125行→ String queryString = "from User as model where model."
+ propertyName + "= ?";
Query queryObject = getCurrentSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
加上包名
String queryString = “from com.bysj.dao.entity.User as model where model.”
修改后如下:
public List findByProperty(String propertyName, Object value) {
log.debug("finding User instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from com.bysj.dao.entity.User as model where model."
+ propertyName + "= ?";
Query queryObject = getCurrentSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
参考:
1、http://blog.csdn.net/shy_snow/article/details/6497219
2、http://bbs.csdn.net/topics/390977299
具体原因待再查好资料补上