org.hibernate.hql.internal.ast.QuerySyntaxException [英] org.hibernate.hql.internal.ast.QuerySyntaxException
问题描述
我有一个功能正在尝试使用休眠模式从数据库中检索一些数据.我正在
I am having one function which is trying to retrieve some data from database using hibernate. I am getting
org.hibernate.hql.internal.ast.QuerySyntaxException:用户未映射[FROM用户,其中id =:id]
org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [FROM User where id = :id]
@Override
public List<User> getUserById(int id) {
System.out.println(id);
Query query = sessionFactory.getCurrentSession().createQuery("FROM User where id = :id");
query.setInteger("id", id);
return query.list();
}
id通过URL传递并由控制器类接收,该控制器类进一步调用了我上面的函数.
The id is passing via URL and receiving it by a controller class which further calling my above function.
查询似乎正确,无法理解为什么会收到此错误.
It seems that query is correct, not able to understand why getting this error.
请提供您的建议. 提前致谢.
Please provide your suggestion on same. Thanks in Advance.
推荐答案
您需要将User
类添加到hibernate.cfg.xml
.
更新
您的HQL查询正确.如果hibernate.cfg.xml
中已经有User
类,请尝试用此替换HQL
Your HQL query is correct. If you already has User
class in hibernate.cfg.xml
, try to replace HQL with this
return (User) session.get(User.class, id);
您需要添加与交易相关的代码.
And you need to add a transaction related code.
这篇关于org.hibernate.hql.internal.ast.QuerySyntaxException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!