Tuesday, November 5, 2013

QuerySyntaxException: Entity is not mapped [from Entity]

Few days ago I was encountering this weird error in my program. This is my first time seeing this error and really nice to meet this error.
org.hibernate.hql.ast.QuerySyntaxException: Entity is not mapped [from Entity]
 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:256)
 at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:187)
 at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
 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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
...
What does this error means? I have this nonsense error struggling me for few hours. I am sure the entity mapping is doing so well in Hibernate, and there is noting wrong with the entity class. After few hours searching, I found out this error was cause by auto-import=”false” as shown in the snapshot below.

<hibernate-mapping package="org.huahsin" auto-import="false">

According to the documentation, I got this:
auto-import (optional - defaults to true): specifies whether we can use unqualified class names of classes in this mapping in the query language.
Let's digest what this message trying to tell. If I got an Entity class declare as below:
    package org.huahsin;

    public class Entity {
    }
    ...
It wouldn’t cause any error if this class is going to make a query like this way:

List<Entity> entityList = session.createQuery("from Entity").list();

This is due to the reason Hibernate use unqualified class name (when auto-import is missing in the Hibernate configuration). Since I have it explicitly set to false, meaning I’m require to put the fully qualified class name when making the query. Thus the correct syntax making this query should be following way:

List<Entity> entityList = session.createQuery("from org.huahsin.Entity").list();

No comments: