为 HQL 配置 Hibernate
-
21-12-2019 - |
题
我在最简单的查询中收到以下错误:
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.internal
的 Hibernate 4.x-hibernate-core-4.2.6.Final.jar
.
Hibernate 上的其他一切都工作正常。但是,我无法运行查询。
我缺少什么?
感谢帮助。
//=========================
编辑:
<table_name>
被定义为一个实体 hibernate.cfg.xml
和 一切 但 HQL——包括更新 <table_name>
工作时没有错误。
解决方案
我想到了两种可能性
您在 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].
其他提示
该错误应该主要来自 query itself
. 。你应该尝试并确保名称 Entity
您正在执行的请求与名称完全相同 class
不喜欢的名字 SQL table
与之相关。
不隶属于 StackOverflow