无法从 Hibernate 工具执行简单的 HQL 查询

Posted

技术标签:

【中文标题】无法从 Hibernate 工具执行简单的 HQL 查询【英文标题】:Can't execute a simple HQL query from Hibernate Tools 【发布时间】:2013-03-14 22:48:00 【问题描述】:

尝试从 Eclipse (Juno) 中的 HQL 编辑器(休眠工具)执行简单的 HQL 查询(来自 Famille)时遇到以下错误:

org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your mysql server version for the right syntax to use near '.famille famille0_' at line 1

这里是详细的错误:

org.hibernate.exception.SQLGrammarException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.famille famille0_' at line 1
at org.hibernate.exception.internal.SQLStateConverter.convert(SQLStateConverter.java:100)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy29.executeQuery(Unknown Source)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1978)
at org.hibernate.loader.Loader.doQuery(Loader.java:829)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
at org.hibernate.loader.Loader.doList(Loader.java:2463)
at org.hibernate.loader.Loader.doList(Loader.java:2449)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
at org.hibernate.loader.Loader.list(Loader.java:2274)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115)
at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
at org.jboss.tools.hibernate4_0.HQLQueryPage.getList(HQLQueryPage.java:58)
at org.hibernate.eclipse.console.views.QueryPageViewer$ContentProviderImpl.getElements(QueryPageViewer.java:120)
at org.eclipse.jface.viewers.StructuredViewer.getRawChildren(StructuredViewer.java:1010)
at org.eclipse.jface.viewers.ColumnViewer.getRawChildren(ColumnViewer.java:703)
at org.eclipse.jface.viewers.AbstractTableViewer.getRawChildren(AbstractTableViewer.java:1087)
at org.eclipse.jface.viewers.StructuredViewer.getFilteredChildren(StructuredViewer.java:917)
at org.eclipse.jface.viewers.StructuredViewer.getSortedChildren(StructuredViewer.java:1067)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefreshAll(AbstractTableViewer.java:701)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:649)
at org.eclipse.jface.viewers.AbstractTableViewer.internalRefresh(AbstractTableViewer.java:636)
at org.eclipse.jface.viewers.AbstractTableViewer$2.run(AbstractTableViewer.java:592)
at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1443)
at org.eclipse.jface.viewers.StructuredViewer.preservingSelection(StructuredViewer.java:1404)
at org.eclipse.jface.viewers.AbstractTableViewer.inputChanged(AbstractTableViewer.java:590)
at org.eclipse.jface.viewers.ContentViewer.setInput(ContentViewer.java:280)
at org.eclipse.jface.viewers.StructuredViewer.setInput(StructuredViewer.java:1690)
at org.hibernate.eclipse.console.views.QueryPageViewer.createTable(QueryPageViewer.java:225)
at org.hibernate.eclipse.console.views.QueryPageViewer.createControl(QueryPageViewer.java:203)
at org.hibernate.eclipse.console.views.QueryPageViewer.<init>(QueryPageViewer.java:160)
at org.hibernate.eclipse.console.views.QueryPageTabView.rebuild(QueryPageTabView.java:109)
at org.hibernate.eclipse.console.views.QueryPageTabView$1.contentsChanged(QueryPageTabView.java:75)
at org.hibernate.eclipse.console.views.QueryPageTabView$1.intervalAdded(QueryPageTabView.java:85)
at javax.swing.AbstractListModel.fireIntervalAdded(Unknown Source)
at org.hibernate.console.QueryPageModel.add(QueryPageModel.java:67)
at org.hibernate.console.KnownConfigurations$1.queryPageCreated(KnownConfigurations.java:88)
at org.hibernate.console.ConsoleConfiguration.fireQueryPageCreated(ConsoleConfiguration.java:332)
at org.hibernate.console.ConsoleConfiguration.executeHQLQuery(ConsoleConfiguration.java:301)
at org.hibernate.eclipse.hqleditor.HQLEditor.executeQuery(HQLEditor.java:447)
at org.hibernate.eclipse.console.actions.ExecuteQueryAction.execute(ExecuteQueryAction.java:88)
at org.hibernate.eclipse.console.actions.ExecuteQueryAction.run(ExecuteQueryAction.java:56)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.hibernate.eclipse.console.actions.ExecuteQueryAction.runWithEvent(ExecuteQueryAction.java:60)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$6.handleEvent(ActionContributionItem.java:452)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.famille famille0_' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2281)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 75 more

编辑:

Famille.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 25 mars 2013 11:54:17 by Hibernate Tools 4.0.0 -->
<hibernate-mapping>
<class name="beans.Famille" table="famille" catalog="bd">
    <id name="familleId" type="java.lang.Integer">
        <column name="familleID" />
        <generator class="identity" />
    </id>
    <property name="libelle" type="string">
        <column name="libelle" not-null="true" unique="true" />
    </property>
    <set name="articles" table="article" inverse="true" lazy="true" fetch="select">
        <key>
            <column name="familleID" />
        </key>
        <one-to-many class="beans.Article" />
    </set>
</class>
</hibernate-mapping>

生成的 SQL 查询:

0: beans.Famille
-----------------
select
  famille0_.familleID as familleID103_,
  famille0_.libelle as libelle103_ 
 from
  bd.bd.famille famille0_

我正在使用带有 MySQL 5 (InnoDB) 方言的 Hibernate 4,

请问我错过了什么?

【问题讨论】:

能否编辑相关实体映射和您尝试执行的 HQL 查询? 嗨,老实说,你的问题很糟糕,虽然很多人想帮助你,但你什么也没告诉我们。 @Alexey 尝试任何查询都会给我一个错误,我提到:来自城镇 @XaviLópez 我编辑了我的问题,查询它是:来自 Town @OQJF 我没有提供太多细节,因为映射在我的应用程序中正常工作,我只在 Eclipse 的 HQL 编辑器中遇到了这个问题 【参考方案1】:

如果查询是简单的from Town,并且您收到此异常,则表示您的实体的映射与您数据库中的列名不匹配。

如果您向我们提供实体的代码及其映射、数据库表定义和生成的 SQL 查询,我们只能提供更多帮助。

【讨论】:

我使用 Hibernate Tools(版本 4)和 MySQL5(innodb)作为方言进行了逆向工程,我刚刚检查了我的 hbm 文件和相应的表,它们看起来不错 错误信息中有对列 .famille 的访问。您的映射中没有粉彩列。如果你需要帮助,为什么不做你被问到的事情?显示实体的代码、它的映射以及生成的 SQL 查询。 如何获取生成的SQL查询? 在eclipse的“Hibernate Dynamic SQL Preview”选项卡中。 谢谢。我添加了生成的sql查询【参考方案2】:

当我使用 Hibernate Tools 从我的数据库中生成域代码和 hbm 文件时,为我的所有映射文件设置了目录属性,因此当我执行查询时,生成的 SQL 会查找 bd.bd.table 而不是 bd .table。

所以我从所有映射文件中删除了目录属性

<hibernate-mapping>
<class name="beans.Famille" table="famille">
//...
</class>
</hibernate-mapping>

现在生成的代码如下所示:

HQL:

from Famille

SQL:

select
  famille0_.familleID as familleID128_,
  famille0_.libelle as libelle128_ 
 from
  bd.famille famille0_

【讨论】:

以上是关于无法从 Hibernate 工具执行简单的 HQL 查询的主要内容,如果未能解决你的问题,请参考以下文章

HQL 异常 (org.hibernate.dialect.Dialect$3)

Hibernate HQL基础

如何在 Hibernate 中执行非多态 HQL 查询?

hibernate的分页怎么用?

Hibernate查询(HQL——Hibernate Query Language)

hibernate hql update操作