线程“main”中的异常java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException获取此Excdption

时间:2018-02-26 06:58:07

标签: java mysql hibernate hibernate-5.x

我正在尝试使用hibernate的简单程序但是从db中检索数据时我收到此错误。但插入工作正常。

我在Bean类中正确提供了所有注释。

任何帮助都会有用。

这是Bean类:

    @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";
        }
    }

测试者类:

    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配置:

<?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>

错误日志:

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

谢谢。 问候, Himangshu。

2 个答案:

答案 0 :(得分:3)

它说“引起:org.hibernate.hql.internal.ast.QuerySyntaxException:testtab未映射”。 Hibernate识别类名,而不是表名。所以

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

坚持

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

答案 1 :(得分:-1)

尝试使用此功能 选择* FROM testtab