Why does Hibernate query have compile error in Int

2019-01-18 03:47发布

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:

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

Why is IntelliJ complaining??

4条回答
Bombasti
2楼-- · 2019-01-18 04:10

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 !).

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

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

查看更多
做个烂人
3楼-- · 2019-01-18 04:11

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

查看更多
相关推荐>>
4楼-- · 2019-01-18 04:15

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.

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.

查看更多
贪生不怕死
5楼-- · 2019-01-18 04:19

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

查看更多
登录 后发表回答