Pages

Saturday, February 25, 2012

Exception: table is not mapped


INFO: HHH000397: Using ASTQueryTranslatorFactory
org.hibernate.hql.internal.ast.QuerySyntaxException: users is not mapped [from users]
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:110)
at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3252)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:215)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:193)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1649)
at com.GamersCom.dao.UserDAO.listUser(UserDAO.java:27)
at com.GamersCom.app.main.main(main.java:22)


User.hbm.xml
<hibernate-mapping package="com.GamersCom.domain">
    <class name="User" table="users" >
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="identity" />
        </id>      
    </class>
</hibernate-mapping>

UserDAO.java

public List<User> listUser() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
List users = null;
try{
transaction = session.beginTransaction();
users = session.createQuery("from users").list();
transaction.commit();
} catch (HibernateException e) {
if (transaction != null )
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
return users;
}



Solution:

In HQL you work with objects not with tables!

users = session.createQuery("from User").list();


1 comment: