Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Hibernate query have compile error in IntelliJ?

Tags:

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

like image 897
Marcus Leon Avatar asked Feb 02 '10 22:02

Marcus Leon


People also ask

How do I run Hibernate program in IntelliJ?

Create a new Java Enterprise project with HibernateClick New Project on the Welcome screen or select File | New | Project. From the Generators list, select Jakarta EE. Name the new project, select a build tool, a language you want to use, and select the Web application project template.

How add Hibernate CFG XML in IntelliJ?

Right click on the desired module -> Add -> Hibernate. Select the newly created Hibernate configuration option, and click the (+) sign in the right pane to create hibernate. cfg. xml file.


2 Answers

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.

like image 78
tumanov Avatar answered Oct 16 '22 22:10

tumanov


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.

like image 24
Michael Neale Avatar answered Oct 17 '22 00:10

Michael Neale