hibernatehqlhibernate.cfg.xml

Configuring Hibernate for HQL


I am getting the following error to the simplest query:

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

In hibernate.cfg.xml I have the property;

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

ThreadLocalSessionContext is in org.hibernate.context.internal of Hibernate 4.x-hibernate-core-4.2.6.Final.jar.

Everything else on Hibernate is working fine. However, i'm not being able to run a query.

What am i missing?

Thanks for help.

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

EDIT:

<table_name> is defined as an entity in hibernate.cfg.xml and everything but HQL-- including the updates on <table_name> are working with no errors.


Solution

  • There are two possibilities which comes to my mind

    1. The name you are referring in your HQL is not an Entity. For e.g.,

      HQL is "Select testEntity from TestEntity testEntity" , however there is no entity class named TestEntity defined

    or

    1. You have defined TestEntity but it is not managed. You say that other operation in entity are working, it looks like you are probably using wrong name in your HQL. However if you want to be sure that the Entity is managed, you can enable the Hibernate debug logging and you should see information like below in which you can see the list of managed classes.

      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].