简体   繁体   English

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

[英]Why does Hibernate query have compile error in IntelliJ?

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 抱怨?

Add a "Hibernate" Facet under "Project Structure", then for that Facet select the "hibernate.cfg.xml" file for "Hibernate Configuration".在“项目结构”下添加“休眠”构面,然后为该构面选择“休眠配置”的“hibernate.cfg.xml”文件。 This will let IDEA know about your class to table mapping and will help it recognize those classes in HQL queries.这将使 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 可能就足够了。

IntelliJ is trying to validate your HQL query inside the string itself. IntelliJ 正在尝试在字符串本身内验证您的 HQL 查询。 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 !).为此,它需要配置为了解您的休眠配置,以确保存在 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.如果它的障碍多于帮助,则可能有一种方法可以将其关闭。

Make sure that you have your Facets configured correctly.确保您的 Facet 配置正确。 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.我的 Spring Data JPA @Query 注释中出现错误“无法解析符号”(仅在 IntelliJ IDEA 中;当我运行它时代码本身运行良好),我需要进入项目结构、方面并添加 JPA方面。 (I assume if one is using Hibernate without JPA, then one would just add the Hibernate facet rather than the JPA facet.) (我假设如果一个人在没有 JPA 的情况下使用 Hibernate,那么人们只会添加 Hibernate 方面而不是 JPA 方面。)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM