为HQL配置Hibernate

为HQL配置Hibernate,hibernate,hql,hibernate.cfg.xml,Hibernate,Hql,Hibernate.cfg.xml,我在最简单的查询中遇到以下错误: org.hibernate.hql.internal.ast.QuerySyntaxException: <table_name> is not mapped [<query_string>] ThreadLocalSessionContext位于hibernate 4.x-hibernate-core-4.2.6.Final.jar的org.hibernate.context.internal中 Hibernate上的其他一切都正常工

我在最简单的查询中遇到以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: <table_name> is not mapped [<query_string>]
ThreadLocalSessionContext位于hibernate 4.x-hibernate-core-4.2.6.Final.jar的org.hibernate.context.internal中

Hibernate上的其他一切都正常工作。然而 我无法运行查询

我错过了什么

谢谢你的帮助

//=========================

编辑:


在hibernate.cfg.xml中定义为一个实体,除HQL之外的所有内容(包括上的更新)都可以正常工作

我想到了两种可能性

您在HQL中引用的名称不是实体。例如

HQL是从testEntity testEntity中选择testEntity,但是没有定义名为testEntity的实体类

您已经定义了TestEntity,但它没有被管理。您说实体中的其他操作正在工作,看起来您可能在HQL中使用了错误的名称。 但是,如果您想确保实体是托管的,您可以启用Hibernate调试日志记录,您应该看到如下信息,在这些信息中可以看到托管类的列表

01:09:40,322 DEBUG HibernatePersistenceProvider:110 - Checking persistence-unit [name=persistence-unit_test, explicit-provider=org.hibernate.ejb.HibernatePersistence] against incoming persistence unit name [persistence-unit_demo]
01:09:40,322 DEBUG ProviderChecker:106 - Persistence-unit [persistence-unit_demo] requested PersistenceProvider [org.hibernate.ejb.HibernatePersistence]
01:09:40,322 DEBUG LogHelper:117 - PersistenceUnitInfo [
    name: persistence-unit_demo
    persistence provider classname: org.hibernate.ejb.HibernatePersistence
    classloader: null
    excludeUnlistedClasses: false
    JTA datasource: null
    Non JTA datasource: null
    Transaction type: RESOURCE_LOCAL
    PU root URL: file:/E:/Test/target/classes/
    Shared Cache Mode: null
    Validation Mode: null
    Jar files URLs []
    Managed classes names [
        com.test.model.TestEntity

    Mapping files names []
    Properties [
        hibernate.c3p0.timeout: 100
        hibernate.connection.autocommit: false
        hibernate.connection.driver_class: com.mysql.jdbc.Driver
        hibernate.c3p0.max_statements: 0
        hibernate.c3p0.max_size: 50
        hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        hibernate.c3p0.idle_test_period: 100
        hibernate.c3p0.min_size: 5
        hibernate.connection.username: XXXX
        hibernate.hbm2ddl.auto: update
        hibernate.c3p0.acquire_increment: 5
        hibernate.connection.url: jdbc:mysql://localhost:3306/test
        hibernate.connection.password: XXXX
        hibernate.show_sql: true
        hibernate.connection.provider_class: org.hibernate.connection.C3P0ConnectionProvider]
01:09:40,337 DEBUG IntegratorServiceImpl:63 - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator].
01:09:40,353 DEBUG IntegratorServiceImpl:63 - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator].

错误应该主要来自查询本身。您应该尝试确保执行请求的实体的名称与类的名称完全相同,而不是与相关联的SQL表的名称相同。

您缺少的是错误消息,以及HQL从不使用表名和列名,而是始终使用实体名和字段/属性名的事实。is not mapepd意味着没有命名为的已知休眠实体。为什么不发布实际的代码和实际的查询?在出现错误之前,您能否向我们提供您试图执行的查询?
01:09:40,322 DEBUG HibernatePersistenceProvider:110 - Checking persistence-unit [name=persistence-unit_test, explicit-provider=org.hibernate.ejb.HibernatePersistence] against incoming persistence unit name [persistence-unit_demo]
01:09:40,322 DEBUG ProviderChecker:106 - Persistence-unit [persistence-unit_demo] requested PersistenceProvider [org.hibernate.ejb.HibernatePersistence]
01:09:40,322 DEBUG LogHelper:117 - PersistenceUnitInfo [
    name: persistence-unit_demo
    persistence provider classname: org.hibernate.ejb.HibernatePersistence
    classloader: null
    excludeUnlistedClasses: false
    JTA datasource: null
    Non JTA datasource: null
    Transaction type: RESOURCE_LOCAL
    PU root URL: file:/E:/Test/target/classes/
    Shared Cache Mode: null
    Validation Mode: null
    Jar files URLs []
    Managed classes names [
        com.test.model.TestEntity

    Mapping files names []
    Properties [
        hibernate.c3p0.timeout: 100
        hibernate.connection.autocommit: false
        hibernate.connection.driver_class: com.mysql.jdbc.Driver
        hibernate.c3p0.max_statements: 0
        hibernate.c3p0.max_size: 50
        hibernate.dialect: org.hibernate.dialect.MySQL5InnoDBDialect
        hibernate.c3p0.idle_test_period: 100
        hibernate.c3p0.min_size: 5
        hibernate.connection.username: XXXX
        hibernate.hbm2ddl.auto: update
        hibernate.c3p0.acquire_increment: 5
        hibernate.connection.url: jdbc:mysql://localhost:3306/test
        hibernate.connection.password: XXXX
        hibernate.show_sql: true
        hibernate.connection.provider_class: org.hibernate.connection.C3P0ConnectionProvider]
01:09:40,337 DEBUG IntegratorServiceImpl:63 - Adding Integrator [org.hibernate.cfg.beanvalidation.BeanValidationIntegrator].
01:09:40,353 DEBUG IntegratorServiceImpl:63 - Adding Integrator [org.hibernate.secure.spi.JaccIntegrator].