Why does Hibernate query have compile error in Int

2019-01-18 03:21发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

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:

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