Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.hibernate.hql.ast.QuerySyntaxException: tablename is not mapped

Tags:

hibernate

i am facing exception: org.hibernate.hql.ast.QuerySyntaxException: Student6 is not mapped [from Student6 stud] my table name is Student6 in sql server database and pojo class name is Student.

    public static void main(String[] args) {
    Configuration configuration = new Configuration();
    SessionFactory  sessionFactory = configuration.configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    try {
        String SQL_QUERY ="from Student6 stud";
             Query query = session.createQuery(SQL_QUERY);
             for(Iterator it=query.iterate();it.hasNext();)             {
             Object[] row = (Object[]) it.next();
             System.out.println("STUDENT_ID: " + row[0]);
             System.out.println("STUDENT_NAME: " + row[1]);
             System.out.println("ADDRESS_STREET: " + row[2]);
             System.out.println("ADDRESS_CITY: " + row[3]);
             System.out.println("ADDRESS_STATE: " + row[4]);
             System.out.println("ADDRESS_ZIPCODE: " + row[5]);                               }

    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
}
like image 620
user783160 Avatar asked Aug 17 '11 16:08

user783160


1 Answers

Your query is not a SQL query. It's a HQL query. It thus should not use table names, but entity class names (from Student instead of from Student6). And it won't return rows in the form of Object[] instances, but will return entity instances.

Hibernate is an ORM: an Object Relational Mapper. The idea is to use objects rather than relational data. You should re-read the Hibernate reference manual.

like image 171
JB Nizet Avatar answered Sep 18 '22 03:09

JB Nizet