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

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

我正在尝试一个简单的休眠程序,但是在从数据库中检索数据时出现此错误。但插入工作正常。

我已经在 Bean 中正确提供了所有注释 class。

任何帮助都会有用。

这是 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";
        }
    }

测试员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(); 
          }

休眠配置:

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

谢谢。 问候, 喜芒鼠.

它说 "Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: testtab is not mapped"。 Hibernate 识别 Class 名称,而不是 table 名称。所以

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

应该是

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

试试用这个 Select * FROM testtab

首先确保HQL中的命名。

在 HQL 查询中使用实体名称。如果您未在 @Entity 注释中指定任何名称,则默认为您的 class 名称。

更多信息:https://javabydeveloper.com/org-hibernate-hql-internal-ast-querysyntaxexception-entity-table-is-not-mapped/