【问题标题】:Configuring Hibernate for HQL为 HQL 配置 Hibernate
【发布时间】:2014-04-13 10:04:42
【问题描述】:

最简单的查询出现以下错误:

org.hibernate.hql.internal.ast.QuerySyntaxException: <table_name> is not mapped [<query_string>]

hibernate.cfg.xml我有财产;

<property name="current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>

ThreadLocalSessionContext 位于org.hibernate.context.internalHibernate 4.x-hibernate-core-4.2.6.Final.jar 中。

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

我错过了什么?

感谢您的帮助。

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

编辑:

&lt;table_name&gt; 被定义为 hibernate.cfg.xmleverything 中的一个实体,但 HQL——包括 &lt;table_name&gt; 上的更新都在正常工作,没有错误。

【问题讨论】:

  • 您缺少的是错误消息,以及 HQL 从不使用表名和列名,而是始终使用实体名和字段/属性名这一事实。 is not mapepd 意味着 Hibernate 不知道名为 的实体。 docs.jboss.org/hibernate/core/4.3/manual/en-US/html/ch16.html
  • 为什么不发布实际代码和实际查询?
  • 您能否在出现该错误之前向我们提供您尝试执行的查询?

标签: hibernate hql hibernate.cfg.xml


【解决方案1】:

我想到了两种可能性

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

    HQL 是 "Select testEntity from TestEntity testEntity" ,但是没有定义名为 TestEntity 的实体类

  1. 您已经定义了 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].
    

【讨论】:

    【解决方案2】:

    错误应该主要来自query itself。您应该尝试确保您正在执行请求的Entity 的名称与class 的名称完全相同,而不是与其关联的SQL table 的名称。

    【讨论】:

      猜你喜欢
      • 2023-04-07
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 2013-08-25
      • 2016-11-23
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多