'Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException getting this Excdption

I am trying a simple program with hibernate but while retrieving data from db i am getting this error. But the insertion is working fine.

I have provided the all the annotations properly in the Bean class.

Any help will be useful.

This is the Bean Class:

    @Entity
    @Table(name = "testtab")
    public class Testtab 
    {
        @Id
        @Column(name = "Id")
        private String id;
        @Column(name = "FirstName")
        private String firstName;
        public String getId() 
        {
            return id;
        }
        public void setId(String id) 
        {
            this.id = id;
        }
        public String getFirstName() 
        {
            return firstName;
        }
        public void setFirstName(String firstName) 
        {
            this.firstName = firstName;
        }
        public String toString()
        {
            return "ID: "+this.getId()+"Name: "+this.getFirstName()+"\n";
        }
    }

The Tester Class:

    public static void main(String[] args) 
    {
        System.out.println("Came in");
        SessionFactory factory = HibernateUtil.getSessionFactory();
        Session session = factory.openSession();

        System.out.println("Came init");
        Transaction tx = null;
        try {
             tx  = session.beginTransaction();
             Testtab test = new Testtab();
             test.setId("e7");
             test.setFirstName("SomeName3");
             System.out.println(session.save(test));
             tx.commit();
          } catch (HibernateException e) {
             if (tx!=null) tx.rollback();
             e.printStackTrace(); 
          } finally {
             session.close(); 
          }
        System.out.println("Done insert");
        session = factory.openSession();
        try {
             tx  = session.beginTransaction();
             List<Testtab> notes = session.createQuery("FROM testtab",Testtab.class).list(); 
             for (Iterator<Testtab> iterator = notes.iterator(); iterator.hasNext();){
                 Testtab note = iterator.next(); 
                 System.out.println(note);
             }
             tx.commit();
          } catch (HibernateException e) {
             if (tx!=null) tx.rollback();
             e.printStackTrace(); 
          } finally {
             session.close(); 
          }

hibernate Config:

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>

      <property name = "hibernate.dialect">
         org.hibernate.dialect.MySQLDialect
      </property>

      <property name = "hibernate.connection.driver_class">
         com.mysql.jdbc.Driver
      </property>

      <!-- Assume test is the database name -->

      <property name = "hibernate.connection.url">
         jdbc:mysql://localhost:3306/alex_notes
      </property>

      <property name = "hibernate.connection.username">
         root
      </property>

      <property name = "hibernate.connection.password">
         password
      </property>

Error Log:

Came in
Feb 26, 2018 12:18:02 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.13.Final}
Feb 26, 2018 12:18:02 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Feb 26, 2018 12:18:03 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Feb 26, 2018 12:18:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Feb 26, 2018 12:18:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/alex_notes]
Feb 26, 2018 12:18:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Feb 26, 2018 12:18:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Feb 26, 2018 12:18:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Feb 26, 2018 12:18:03 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Feb 26, 2018 12:18:03 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
Came init
e7
Done insert
Feb 26, 2018 12:18:04 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Exception in thread "main" java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: testtab is not mapped [FROM testtab]
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:686)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:103)
    at notesapi.test.dao.Tester.main(Tester.java:40)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: testtab is not mapped [FROM testtab]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:553)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:662)
    ... 3 more
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: testtab is not mapped
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:171)
    at org.hibernate.hql.internal.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:91)
    at org.hibernate.hql.internal.ast.tree.FromClause.addFromElement(FromClause.java:79)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:326)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3706)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3595)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:720)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:576)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:313)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:261)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
    ... 9 more

Thank you. Regards, Himangshu.



Solution 1:[1]

It says "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: testtab is not mapped". Hibernate recognizes Class name, not table name. So

List<Testtab> notes = session.createQuery("FROM testtab",Testtab.class).list();

shoulde be

List<Testtab> notes = session.createQuery("FROM Testtab",Testtab.class).list();

Solution 2:[2]

First of you have ensure the naming in HQL.

Use the name of the entity in the HQL query. If you don't specify any name in the @Entity annotation then the default is your class name.

enter image description here

For More Information: https://javabydeveloper.com/org-hibernate-hql-internal-ast-querysyntaxexception-entity-table-is-not-mapped/

Solution 3:[3]

try using this Select * FROM testtab

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bejond
Solution 2 Mahbub Ul Islam
Solution 3 Imran S