org.hibernate.hql.ast.QuerySyntaxException Solution

(76 Views)


In this article, we are discussing the hibernate org.hibernate.hql. ast.QuerySyntaxException Solution. This exception is very common if you are a beginner creating hibernate application.

Exception Stacktrace:

Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: employee is not mapped [from employee] at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180) at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111) at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93) at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:327) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3441) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3325) at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:733) at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:584) at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301) at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:244) at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124) at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770) at com.payroll.EmployeeDeatails.main(EmployeeDeatails.java:21)

Cause Of Exception

The above QuerySyntaxException is thrown when we are not mapping the table name properly. The new comers in hibernate may face this error because you people will map the table name directly in your query. That query will not work in the hibernate framework. You will have to map the class name which is mapped in the Hibernate configuration file. Refer the following example:

Employee Entity:

@Entity @Table(name = "employee", schema = "payroll") public class Employee{ int id; String name; String department; //getters and setters }

Hibernate Code:

public static void main(String[] args) { //Creating session factory object SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); //Getting session object from session factory object Session session = sessionFactory.openSession(); //Getting transaction object from session object session.beginTransaction(); Query query = session.createQuery("from employee"); List<Employee> employees = query.list(); for(Employee emp1 : employees) { System.out.println("eid: "+emp1.getId()+", Employee Name: "+emp1.getName()+", Department: "+emp1.getDepartment()); } session.getTransaction().commit(); sessionFactory.close(); }

Description

This query is not a SQL query. Here you are using HQL query. So you should not use table names, but entity class names ("from Employee" instead of "from employee"). Here "employee" is not entity name by default entity name is same as class name so we have to use "Employee" instead of "employee". HQL picks the entity name not table name automatically. So please be careful about this concern.

Query query = session.createQuery("from employee"); // Exception is thrown

Solution

If you using the following code then you got successfully running.

public static void main(String[] args) { //Creating session factory object SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); //Getting session object from session factory object Session session = sessionFactory.openSession(); //Getting transaction object from session object session.beginTransaction(); Query query = session.createQuery("from Employee");//Here the class names is case sensitive. List<Employee> employees = query.list(); for(Employee emp1 : employees) { System.out.println("eid: "+emp1.getId()+", Employee Name: "+emp1.getName()+", Department: "+emp1.getDepartment()); } session.getTransaction().commit(); sessionFactory.close(); }

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search