JPA Cannot resolve column/IntelliJ
JavaHibernateJpaIntellij IdeaMappingJava Problem Overview
I'm trying to map some Java classes using the Hibernate JPA implementation. My problem is that I can't use hardcoded Strings als column namens. You can see the error message I get in the picture below.
I'm using OpenJPA as my Default JPA Provider and have already tried to change it.
IntelliJ Version 14.0.3
regards,
Java Solutions
Solution 1 - Java
You have to associate your data source (create a data source in database view first - aka your real JDBC database connection) with a persistence unit or session factory (hibernate.cfg.xml).
Follow below steps and the warnings will disappear:
- Open the Persistence tool window (View | Tool Windows | Persistence).
- Right-click the necessary module, persistence unit or session factory, and select Assign Data Sources in the context menu.
- In the Assign Data Sources dialog that opens, click the Data Source
field and select the necessary data source. (To remove the
association with the data source, select
.)
See here: Associating data sources with session factories and persistence units
Solution 2 - Java
For those who just want to disable this check, go to
Intellij IDEA -> Preferences -> Search "Unresolved database references in annotations" and uncheck it.
(macOS Mojave and Intellij Ultimate 2019.3)
This will disable the inspection and remove all the "Cannot resolve column…" errors on the @Column
annotations. Intellij will stop checking if the String column names exist in the database tables. Do it at your own risk.
Solution 3 - Java
I found Panos' answer useful, but I didn't see anybody mention adding the database as a data source. Perhaps that's routine enough to be assumed, but if not, here's what I had to do:
Select View/Tool Windows/Database
The Database window usually appears on the top right.
In the Database window, click the green + sign and select Data Source/MySQL (or whatever flavor of Data Source you're using).
The Data Sources and Drivers window pops up.
If your database isn't listed there, use the empty form to add the following:
-
Host: usually localhost, but if your test database is living on another machine, put that address in.
-
Username: for your database use.
-
Password: for your database user's password.
IDEA might need some fiddling to find the JDBC driver. I was told that in theory it should have found it in the maven build process for the project I was working on, but it did not. I reopened View/Tool Windows/Database and looked at my MySQL entry. At the bottom it had an error message next to Driver:, and also a MySQL link. I clicked the MySQL link and IDEA brought up a popup to fetch Connector/J.
Despite the fact that the checkboxes for Auto commit and Auto sync defaulted to checked and I left them that way, IDEA seemed to need a nudge. Or perhaps it was just taking a while and I was impatient.
In any event, first I double-clicked on the line for my database in Database. That seemed to do it, but I didn't realize I needed Persistence yet, and while sorting that out, at a coworker's suggestion, I also clicked the Synchronize button (two arrows in a circle) on Database.
You can also right-click on your database in Database and select Synchronize.
It may take a few seconds, but you should see IDEA filling in the database schema under the entry in Database.
Finally I found Panos's answer and fixed Persistence.
Select View/Tool Windows/Persistence
The Persistence window usually appears on the top left.
In the Persistence window, right-click on your project and select Assign Data Sources.
IDEA pops up a dialog with two columns, your project in the left column and in my case an empty cell in the right column. Click on the empty cell and IDEA should give you a dropdown that allows you to select the database you just added.
Again, it may take a few seconds for IDEA to finish analyzing the data source and redo all the inspections.
Solution 4 - Java
Just for anyone else whom this didn't solve and comes across via google (like myself) .. setting the table name via the @Table Annotation fixed it for me.
Solution 5 - Java
Actually this is not any error that prevents your code to compile. Probably your spell-checker is on and which gives you the spelling mistake. If you can compile your code then you may ignore these type of scenario.
Look you have not getting any error for other text like @Column
, @GeneratedValue
etc. That means jars using these kind of stuff are in your build path. So I think you can ignore these type of error.
Solution 6 - Java
If you are using the JPA, it is also important to set up the SQL Dialect as HSQLDB instead of MySQL. This is often confusing because it feels like MySQL is the correct dialect, but in fact what you are dealing with hibernate is HSQL.
If you have just upgraded to IntelliJ 2017.13 or other derivatives of this version (e.g., Webstorm), or you've just re-imported your project, it might mess up the language injection and need to manually correct this.
This can be accomplished via the Project
> Settings -> Languages and Frameworks
part of the IDE.
Solution 7 - Java
If there is no real mismatch and this warning happens just because of IDE, you can easily link your project with the data source that you connect. Intellij already suggests the way to do it.
Solution 8 - Java
I'm using Intellij and connecting DB by JPA. I've met problem just like you and this is my solution.
- You need to connect your database following these steps:
- View -> Tool Windows -> Database -> then add database you are using to Intellj
- After connect your database, you need to assign data source to Intellj by following these
- View -> Tool Windows -> Persistence -> then add data from database you just added from 1 step OK. That's all! P/s: make sure your code work well!