Java 为什么 Hibernate 查询在 IntelliJ 中有编译错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:32:34  来源:igfitidea点击:

Why does Hibernate query have compile error in IntelliJ?

javahibernateintellij-idea

提问by Marcus Leon

I have this Hibernate code:

我有这个休眠代码:

Query q = session.createQuery("from MyTable where status = :status");

It compiles and works fine..

它编译并运行良好..

But in IntelliJ I get this error reported:

但是在 IntelliJ 中,我报告了这个错误:

Can't resolve expression, Can't resolve symbol 'MyTable'

无法解析表达式,无法解析符号“MyTable”

Why is IntelliJ complaining??

为什么 IntelliJ 抱怨?

采纳答案by Michael Neale

IntelliJ is trying to validate your HQL query inside the string itself. To do this it needs to be configured to know about your hibernate configuration to ensure that a mapping exists for MyTable (it does at runtime, as you know - as it executes !).

IntelliJ 正在尝试在字符串本身内验证您的 HQL 查询。为此,它需要配置为了解您的休眠配置,以确保存在 MyTable 的映射(正如您所知,它在运行时执行 - 在执行时!)。

Check out the hibernate config section in intelliJ for your project.

在您的项目的 intelliJ 中查看 hibernate 配置部分。

There is probably a way of turning it off if it is more hindrance than help.

如果它的障碍多于帮助,则可能有一种方法可以将其关闭。

回答by Bram Luyten

In IntelliJ IDEA 13 for Mac OS X I could find this option as "Query Language Checks" under "JPA Issues"

在 Mac OS XI 的 IntelliJ IDEA 13 中,可以在“JPA 问题”下的“查询语言检查”中找到此选项

回答by Bram Luyten

Make sure that you have your Facets configured correctly. I was getting errors in my Spring Data JPA @Query annotations of "Can't resolve symbol" (just within IntelliJ IDEA; the code itself worked fine when I ran it) and I needed to go into Project Structure, Facets and add the JPA facet. (I assume if one is using Hibernate without JPA, then one would just add the Hibernate facet rather than the JPA facet.)

确保您的 Facet 配置正确。我的 Spring Data JPA @Query 注释中出现错误“无法解析符号”(仅在 IntelliJ IDEA 中;当我运行它时代码本身运行良好),我需要进入项目结构、方面并添加 JPA方面。(我假设如果一个人在没有 JPA 的情况下使用 Hibernate,那么人们只会添加 Hibernate 方面而不是 JPA 方面。)

回答by tumanov

Add a "Hibernate" Facet under "Project Structure", then for that Facet select the "hibernate.cfg.xml" file for "Hibernate Configuration". This will let IDEA know about your class to table mapping and will help it recognize those classes in HQL queries.

在“项目结构”下添加一个“休眠”构面,然后为该构面选择“休眠配置”的“hibernate.cfg.xml”文件。这将使 IDEA 了解您的类到表的映射,并帮助它识别 HQL 查询中的这些类。

If you don't use "hibernate.cfg.xml", for example for Spring you may just use "applicationContext.xml" to initialize your datasource, having Hibernate Facet declared may be enough.

如果您不使用“hibernate.cfg.xml”,例如对于 Spring,您可能只使用“applicationContext.xml”来初始化您的数据源,声明 Hibernate Facet 可能就足够了。

回答by Lemmy

You have to configure support for HQL in Preferences | Editor | Language Injections. This is how I solved the issue: enter image description here

您必须在Preferences | Editor | Language Injections. 我是这样解决这个问题的: 在此处输入图片说明