Sunday, March 4, 2012

Hibernate Error : org.hibernate.hql.ast.QuerySyntaxException: TestTable is not mapped [from TestTable}

Problem : Getting an exception while trying to get all the records from a TestTable.:

Following is the Hibernate Config for TestTable:


<hibernate-mapping>
    <class name="com.testapp.service.dataobjects.TestTableDO" table="testTable">
    <id name="testId" column="testId">
        <generator class="assigned"/>
    </id>
...
...
</class>
</hibernate-mapping>




And Following is the Java code to get all the records from the TestTable

List<TestTableDO> testTableResultSet = hibernateTemplate.find("from TestTable");



Solution: The problem is due to the name mismatch in the Hibernate table configurations and the Java Query.

As you can see the hibernate is configured to map the 'testTable' to the 'com.testapp.service.dataobjects.TestTableDO' whereas in Java it refers to 'TestTable' (database table name).
Because we have mapped it to 'TestTableDO' it should be referred as same in Java Query as follows:



List<TestTableDO> testTableResultSet = hibernateTemplate.find("from TestTableDO");

Cheers :)



Error Logs
org.hibernate.hql.ast.QuerySyntaxException: TestTable is not mapped [from TestTable]
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:265)
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.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(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 org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:923)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:921)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)

No comments:

Post a Comment