0

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: 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();
}

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.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
Amit
  • 37
  • 3
  • 10

2 Answers2

0

You need to add User class to the hibernate.cfg.xml.

Update

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.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Its already added and the other function like getallUser() and addUser() are working fine. Only while getting User by Id , I am facing this issue. – Amit Dec 31 '15 at 08:40
  • Sorry for asking, but what do you mean by transaction related code. What extra thing I need to write now for transaction. – Amit Dec 31 '15 at 08:59
  • @AmitKumar You should use transactions not only for write data, but for read data too. If you don't use declarative transactions (via Spring `@Transactional`, for an example) you need manually `beginTransaction()` and `commit()` it. – v.ladynev Dec 31 '15 at 11:45
0

I had that error and work for me using the complete class name, package+className.

Check other options here org.hibernate.hql.internal.ast.QuerySyntaxException: table is not mapped

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/30981020) – Procrastinator Feb 08 '22 at 07:09