How to solve QuerySyntaxException (table is not mapped) in hibetnate?

The following exception is very common if you are the beginner for hibernate.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: book is not mapped [from book]
	at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
	at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
	at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
	at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:255)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3056)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
	at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
	at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
	at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
	at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
	at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77)
	at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56)
	at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
	at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
	at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
	at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
	at hibernate.HibernateExample.main(HibernateExample.java:15)

The above QuerySyntaxException is thrown when we are not mapping the table name properly. The first time hibernate users
can be seen this error because you will map the table name directly in the query. That will not work in the hibernate. You will have to
map the class name that is mapped in the Hibernate configuration file. Look into the following example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
 
/**
 * source : www.javabeat.net
 */
public class HibernateExample {
    public static void main(String args[]){
        Configuration configuration = new Configuration();
        SessionFactory  sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery("delete from Book where id=5");
        query.executeUpdate();
        transaction.commit();
    }
}

In the above example Book is the class name that is mapped in the hbm file as follows:

Here the class names is case sensitive.

Comments

comments

Comments

  1. Thanks a lot

  2. I came here seeking solution for
    XYZ is not mapped[FROM XYZ ] hibernate exception.

    I got it resolved by using full java class path instead of table name.I though it might be helpful somebody else also.Please look

    http://fundapass.blogspot.in/2012/09/xyz-is-not-mapped-from-xyz.html

    for full description.

  3. you can find some more details in the below link,”http://javadomain.in/solved-exception-in-thread-main-org-hibernate-hql-ast-querysyntaxexception-is-not-mapped/”

  4. gaius gracchus says:

    one thing we noticed is that your sql has to have the table names match the model in CASE exactly — at least for our types of queries

  5. Note: Proposed method is depreciated as of Hibernate 4.3

Speak Your Mind

*